-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyConvertify.py
79 lines (67 loc) · 2.42 KB
/
pyConvertify.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import subprocess
def convert_file(input_file, output_file, output_format):
try:
subprocess.run(['ffmpeg', '-i', input_file, '-c:v', 'libx264', '-c:a', 'copy', output_file], check=True)
print(f"Conversion successful! Output file saved as {output_file}")
except subprocess.CalledProcessError as e:
print("Conversion failed.")
print(e)
def prompt_output_format():
print("Select the output format:")
print("1. MP4")
print("2. AVI")
print("3. MKV")
print("4. WebM")
choice = input("Enter your choice (1-4): ")
formats = {
'1': 'mp4',
'2': 'avi',
'3': 'mkv',
'4': 'webm'
}
if choice in formats:
return formats[choice]
else:
print("Invalid choice. Defaulting to MP4.")
return 'mp4'
def check_file_exists(file_path):
if not os.path.isfile(file_path):
print(f"Error: The file {file_path} does not exist.")
return False
return True
def check_dir_exists(dir_path):
if not os.path.isdir(dir_path):
print(f"Error: The directory {dir_path} does not exist.")
return False
return True
def check_filename_valid(filename):
invalid_chars = ['<', '>', ':', '"', '/', '\\', '|', '?', '*']
if any(char in filename for char in invalid_chars):
print(f"Error: The filename {filename} contains invalid characters.")
return False
return True
def prompt_input_file():
while True:
input_file = input("Enter the input file name: ")
if check_file_exists(input_file):
return input_file
def prompt_output_file():
while True:
output_dir = input("Enter the output directory path: ")
if not check_dir_exists(output_dir):
continue
output_file = input("Enter the output filename (without extension): ")
if not check_filename_valid(output_file):
continue
output_format = prompt_output_format()
output_file_path = f"{output_dir}/{output_file}.{output_format}"
return output_file_path
def main():
input_file = prompt_input_file()
input_file_path = os.path.join(os.getcwd(), input_file) # Assume file is in the current directory
output_file = prompt_output_file()
output_format = output_file.split('.')[-1] # Extract the output format from the file name
convert_file(input_file_path, output_file, output_format)
if __name__ == '__main__':
main()