-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp4-cutter.py
78 lines (62 loc) · 2 KB
/
mp4-cutter.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
import glob, os
from tkinter import filedialog as fd
from multiprocessing.pool import ThreadPool
def getFolder() -> str:
"""
Opens file dialogue
:return : file directory path as string
"""
directory = fd.askdirectory(initialdir="~/")
directory.replace(" ","\\ ")
directory += "/"
print(directory)
return directory
def converter(file:str) -> str:
"""
Conversion subroutine
:param file: string
:return: str containing name of successfully congerted file
"""
cmd = get_command(file)
try:
run_command(cmd)
os.system(f"rm {get_filename(file)}_edit.mp4")
except:
raise SystemError("An error occured when converting file" + file)
return file + " converted succesfully"
def get_command(file:str)->str:
"""
Gets str for appropriate command for ffmpeg and auto editor given the file
"""
filename = get_filename(file)
return f"ffmpeg -i {file} -c:v libx264 {filename}_edit.mp4 && auto-editor {filename}_edit.mp4 --no-open"
def run_command(cmd:str):
"""
Runs the string command
"""
os.system(cmd)
def get_files(directory:str) -> list:
"""Returns a list of filenames given the directory path as a strings"""
os.chdir(directory)
files = glob.glob("*.mp4")
if files is None or len(files) == 0:
raise FileNotFoundError(f"No mp4 files in the directory {directory}")
print(f"Files:")
for f in files:
print(f)
files = [f.replace(" ", "\\ ") for f in files]
return files
def get_filename(file):
return file.split(".")[0]
def main():
directory = getFolder()
files = get_files(directory)
if input("Do you want to convert these files [Y/n]") != "Y":
return
# threading over 4 processes
with ThreadPool(processes=4) as pool:
# call the function for each item concurrently, get results as tasks complete
for result in pool.imap(converter, files):
print(result)
if __name__ == "__main__":
main()