From fcd19b0f983084b743c36c8954f9c7ce0d6238e9 Mon Sep 17 00:00:00 2001 From: "Machado Gamboa, Kevin" Date: Wed, 14 Aug 2024 21:05:33 +0100 Subject: [PATCH 1/2] Enhancement that works --- README.md | 20 ++++++++++ video_to_audio.py | 94 +++++++++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 0f90775..1e3fc10 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,23 @@ $ cd ../Video-to-audio-converter ```bash $ python3 video_to_audio.py ``` + +### Other Prerequisites +Make sure you install ffmpeg (specially on Windows). You may encounter an error referred to ffmpeg not being installed + +#### macOS: +Install FFmpeg using Homebrew: +``` +brew install ffmpeg +``` +#### Linux: +Install FFmpeg using the package manager for your distribution. For example, on Ubuntu, you would run: +``` +sudo apt-get update +sudo apt-get install ffmpeg +``` + #### Verify FFmpeg Installation +``` +ffmpeg -version + +``` \ No newline at end of file diff --git a/video_to_audio.py b/video_to_audio.py index dc01b24..4e700ed 100644 --- a/video_to_audio.py +++ b/video_to_audio.py @@ -1,45 +1,61 @@ #!/usr/bin/env python3 -import urllib.request -import urllib.error -import re -import sys -import time import os -import pipes - -def video_to_audio(fileName): - try: - file, file_extension = os.path.splitext(fileName) - file = pipes.quote(file) - video_to_wav = 'ffmpeg -i ' + file + file_extension + ' ' + file + '.wav' - final_audio = 'lame '+ file + '.wav' + ' ' + file + '.mp3' - os.system(video_to_wav) - os.system(final_audio) - #file=pipes.quote(file) - #os.remove(file + '.wav') - print("sucessfully converted ", fileName, " into audio!") - except OSError as err: - print(err.reason) - exit(1) +import sys +import shlex +import subprocess + + +def video_to_audio(file_path, output_format='mp3'): + try: + # Check the file extension + file_name, file_extension = os.path.splitext(file_path) + + # Properly escape the file names + file_name_escaped = shlex.quote(file_name) + file_path_escaped = shlex.quote(file_path) + + # Set up output file name and command + if output_format == 'wav': + output_file = f"{file_name_escaped}.wav" + command = f"ffmpeg -i {file_path_escaped} {output_file}" + elif output_format == 'mp3': + output_file = f"{file_name_escaped}.mp3" + command = f"ffmpeg -i {file_path_escaped} -q:a 0 {output_file}" + else: + print(f"Unsupported output format: {output_format}") + return + + # Execute the command + print(f"Converting {file_path} to {output_format} format...") + subprocess.run(command, shell=True, check=True) + print(f"Successfully converted {file_path} to {output_file}!") + + except subprocess.CalledProcessError as e: + print(f"An error occurred during conversion: {e}") + sys.exit(1) + except OSError as err: + print(f"OS error: {err}") + sys.exit(1) + def main(): - if len(sys.argv) <1 or len(sys.argv) > 2: - print('command usage: python3 video_to_audio.py FileName') - exit(1) - else: - filePath = sys.argv[1] - # check if the specified file exists or not - try: - if os.path.exists(filePath): - print("file found!") - except OSError as err: - print(err.reason) - exit(1) - # convert video to audio - video_to_audio(filePath) - time.sleep(1) - -# install ffmpeg and/or lame if you get an error saying that the program is currently not installed + if len(sys.argv) != 3: + print('Usage: python3 video_to_audio.py ') + print('OutputFormat should be either "mp3" or "wav"') + sys.exit(1) + else: + file_path = sys.argv[1] + output_format = sys.argv[2] + + # Check if the specified file exists or not + if not os.path.exists(file_path): + print(f"File {file_path} not found!") + sys.exit(1) + + # Convert video to audio + video_to_audio(file_path, output_format) + + if __name__ == '__main__': - main() + main() From c638942c3aad020037918394f1e155b8b955ebcf Mon Sep 17 00:00:00 2001 From: "Machado Gamboa, Kevin" Date: Wed, 14 Aug 2024 21:09:57 +0100 Subject: [PATCH 2/2] updating --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1e3fc10..f2e53f9 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ brew install ffmpeg ``` #### Linux: Install FFmpeg using the package manager for your distribution. For example, on Ubuntu, you would run: + ``` sudo apt-get update sudo apt-get install ffmpeg