MP4_TO_MP3_CONVERTER


The Python program featured in this tutorial converts an MP4 video file to an MP3 audio file (which essentially amounts to isolating the sound track component of the input video file from the moving pictures component). Like the Python program whose Uniform Resource Locator is displayed below, the Python program featured on this web page uses Python modules which are downloaded and installed on the local machine which executes that Python program.

If you would like to shrink an MP4 file to a smaller size, you can use the Python program featured on the tutorial web page at the following Uniform Resource Locator:

https://karbytesforlifeblog.wordpress.com/mp4_video_compressor/

To view hidden text inside each of the preformatted text boxes below, scroll horizontally.


SOFTWARE_APPLICATION_COMPONENTS


python_source_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_24/main/mp4_to_mp3.py


PROGRAM_INTERPRETATION_AND_EXECUTION


STEP_0: Copy and paste the Python source code into a new text editor document and save that document as the following file name:

mp4_to_mp3.py

STEP_1: Install pip (which, for karbytes, required using a virtual environment in which to install and run pip-installed Python modules) using the following Unix commands.

(The purpose of the virtual environment is to prevent conflicts which may arise from different processes using the same computing resources. In this case, the virtual environment enables the user to manage pip-installed packages independently of the system Python environment).

(Note also that installing the virtual environment automatically installs pip with it).

1.0: Create the virtual environment:

python3 -m venv myenv

1.1: Activate the virtual environment:

source myenv/bin/activate

1.2: Install MoviePy within the Virtual Environment:

pip install moviepy

1.3: Use the Virtual Environment:

While the virtual environment is active, any Python commands will use the packages installed in myenv. To deactivate the virtual environment, use the following command:

deactivate

STEP_2: While the virtual environment is active, set the current directory to wherever the Python program file is located on the local machine (e.g. Desktop).

cd Desktop

STEP_3: Run the program by entering the following command:

python3 mp4_to_mp3.py

STEP_4: If the program interpretation command does not work, then use the following commands (in top-down order) to install the Python interpreter:

sudo apt update
sudo apt install python3

STEP_5: Observe program results in the file directory where the input video file is located. The program should have created a folder there named output (if that folder did not exist there already) and generated the output file inside of that folder.


PROGRAM_SOURCE_CODE


Note that the text inside of each of the the preformatted text boxes below appears on this web page (while rendered correctly by the web browser) to be identical to the content of that preformatted text box text’s respective plain-text file or source code output file (whose Uniform Resource Locator is displayed as the green hyperlink immediately above that preformatted text box (if that hyperlink points to a source code file) or whose Uniform Resource Locator is displayed as the orange hyperlink immediately above that preformatted text box (if that hyperlink points to a plain-text file)).

(Note that angle brackets which resemble HTML tags (i.e. an “is less than” symbol (i.e. ‘<‘) followed by an “is greater than” symbol (i.e. ‘>’)) displayed on this web page have been replaced (at the source code level of this web page) with the Unicode symbols U+003C (which is rendered by the web browser as ‘<‘) and U+003E (which is rendered by the web browser as ‘>’). That is because the WordPress web page editor or web browser interprets a plain-text version of an “is less than” symbol followed by an “is greater than” symbol as being an opening HTML tag (which means that the WordPress web page editor or web browser deletes or fails to display those (plain-text) inequality symbols and the content between those (plain-text) inequality symbols)).

python_source_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_24/main/mp4_to_mp3.py


#########################################################################################
# file: mp4_to_mp3.py
# type: Python
# date: 01_NOVEMBER_2024
# author: karbytes
# license: PUBLIC_DOMAIN 
#########################################################################################
from moviepy.editor import VideoFileClip
import os

def mp4_to_mp3(input_file, output_folder="output"):
    # Check if input file exists and is an MP4
    if not os.path.isfile(input_file) or not input_file.endswith('.mp4'):
        print("Please provide a valid MP4 file.")
        return

    # Create output folder if it doesn't exist
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # Extract base filename without extension
    base_filename = os.path.splitext(os.path.basename(input_file))[0]
    output_file = os.path.join(output_folder, f"{base_filename}.mp3")

    # Load the video file and extract audio
    try:
        video_clip = VideoFileClip(input_file)
        audio_clip = video_clip.audio
        audio_clip.write_audiofile(output_file)
        print(f"Successfully converted {input_file} to {output_file}")
    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        # Release resources
        video_clip.close()
        if audio_clip:
            audio_clip.close()

input_mp4 = "input_video.mp4"  # Replace with your MP4 file path
mp4_to_mp3(input_mp4)

This web page was last updated on 07_NOVEMBER_2024. The content displayed on this web page is licensed as PUBLIC_DOMAIN intellectual property.