-
Notifications
You must be signed in to change notification settings - Fork 0
/
autompeg.py
executable file
·61 lines (49 loc) · 2.2 KB
/
autompeg.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
# import all necessarily modules
import os.path
import subprocess
import sys
from configparser import ConfigParser
# working dir and extension types will be passed through CLI
# Config Parser
config = ConfigParser(allow_no_value=True)
try:
with open('config.ini', 'r') as cfg:
config.read_file(cfg)
path = config.get('Path of ffmpeg', 'path')
except IOError:
print("Couldn't find or open configuration file for ffmpeg. Process is exiting now..")
sys.exit()
# exception-clause to prevent a faulty WorkDir and therefore the following ffmpeg process
try:
workDir = sys.argv[1]
extType = sys.argv[2]
newExtType = sys.argv[3]
except IndexError:
raise Exception("Usage: python3 autompeg.py <path to workfolder> <old fileformat> <new fileformat>"
"e.g. (Windows) python3 autompeg.py C:\\Users\\Test\\Work .ts .mp4"
"e.g. (Mac) python3 autompeg.py /Volumes/Volume1/Work .ts .mp4"
"e.g. (Linux) python3 automopeg.py ~/Videos/Work .ts .mp4")
if sys.platform.startswith('win32'):
workDir = workDir.replace('/', '\\')
else:
pass
for root, directories, filenames in os.walk(workDir):
for filename in filenames:
filename = os.path.join(root, filename)
newfilename = os.path.splitext(filename)[0] + newExtType
if filename.endswith(extType): # scan for files with the extension given in 'extType'
filepath = filename
newfilepath = newfilename
# no need to include an exception-clause here yet, since ffmpeg automatically detects a faulty filepath
subprocess.run(
[
path, # path of ffmpeg
"-i", # input argument for file
f'{filepath}', # file path of the old media file
"-c:v", # select video stream
"copy", # copy video stream and don't convert it (to prevent quality loss)
"-bsf:a", # select bitstream filter for the audio stream
"aac_adtstoasc", # remove the ADTS header from the audio stream
f'{newfilepath}', # file path of the 'new' media file
]
)