Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement that works #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ $ cd ../Video-to-audio-converter
```bash
$ python3 video_to_audio.py <filename>
```

### 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

```
94 changes: 55 additions & 39 deletions video_to_audio.py
Original file line number Diff line number Diff line change
@@ -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 <FilePath> <OutputFormat>')
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()