-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathvideo_to_audio.py
61 lines (48 loc) · 1.81 KB
/
video_to_audio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import os
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) != 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()