-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvideo-transcoder-py3.py
More file actions
59 lines (51 loc) · 1.97 KB
/
video-transcoder-py3.py
File metadata and controls
59 lines (51 loc) · 1.97 KB
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
#!/usr/bin/python
import argparse
import sys
import os
import glob
import ffmpy
def create_arg_parser():
"""Creates and returns the ArgumentParser object."""
parser = argparse.ArgumentParser(description='This script allows you to find all .ts files in the inputDirectory and convert them to mp4 files using ffmpeg')
parser.add_argument('inputDirectory',
help='Path to the input directory.')
parser.add_argument('--deleteOriginal', default=False, action="store_true",
help='Decide if you want to delete the original file')
return parser
def list_files(dir):
return glob.glob(dir + "/*.ts")
def transcode_file(inputFile):
outputFile = inputFile[:-2] + "mp4"
ff = ffmpy.FFmpeg(
inputs={inputFile: None},
outputs={outputFile: '-c:v libx264 -strict -2'},
)
print(ff.cmd) # Updated to use parentheses
ff.run()
def check_file(inputFile, deleteOriginal):
if deleteOriginal == False:
return
outputFile = inputFile[:-2] + "mp4"
ofs = os.path.getsize(inputFile)
tfs = os.path.getsize(outputFile)
size_min = ofs * 0.25
size_max = ofs * 1.2
if (tfs < size_min) or (tfs > size_max):
print("Transcoded file not within reasonable size limits")
print("Transcoded file size: " + str(tfs) + " bytes")
print("Limits:\nMin: " + str(size_min) + "\nMax: " + str(size_max))
print("Original file size: " + str(ofs) + " bytes")
os._exit(1)
elif tfs >= size_min and tfs <= size_max:
print("Transcoded file within reasonable size limits")
if deleteOriginal:
os.remove(inputFile)
if __name__ == "__main__":
arg_parser = create_arg_parser()
parsed_args = arg_parser.parse_args(sys.argv[1:])
if os.path.exists(parsed_args.inputDirectory):
files = list_files(parsed_args.inputDirectory)
files.sort()
for f in files:
transcode_file(f)
check_file(f, parsed_args.deleteOriginal)