-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtranscode.py
executable file
·107 lines (97 loc) · 3.08 KB
/
transcode.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#! /usr/bin/python
""" Depends on ffmpeg and MP4Box. This scripts accepts a HD video and creates different
bitrate versions for dash
@author : Hrishikesh Bhaskaran <hrishi.kb@gmail.com>, May 2019
"""
import os
import sys
from xml.dom import minidom
config = {
"keyint": "59",
"framerate": "30000/1001",
"profile": "onDemand",
"chunk": "1000",
}
base_filename = None
# pre-defined resolutions
versions = ["256", "426", "854", "1280", "1920"]
files_to_clean = []
def create_multiple_bitrate_versions(filename):
for version in versions:
command = 'ffmpeg -i {} -vf "hqdn3d,detelecine,yadif,scale={}:-2" -x264-params "keyint={}:min-keyint={}:no-scenecut" -b:a 50k -strict -2 -preset veryfast -crf 25 -r {} {}/{}-{} -y'.format(
filename,
version,
config.get("keyint"),
config.get("keyint"),
config.get("framerate"),
base_filename,
version,
filename,
)
print(command)
os.system(command)
def create_multiple_segments(filename):
os.chdir(base_filename)
base_command = "MP4Box -dash {} -frag {} -rap -frag-rap -profile {} {} {}-{}{}"
for version in versions:
command = base_command.format(
config.get("chunk"),
config.get("chunk"),
config.get("profile"),
"-out " + version + "-" + base_filename + ".mpd",
version,
filename,
"#video",
)
print(command)
os.system(command)
files_to_clean.append(
base_filename + "/" + version + "-" + base_filename + ".mpd"
)
command = base_command.format(
config.get("chunk"),
config.get("chunk"),
config.get("profile"),
"-out audio.mpd",
versions[0],
filename,
"#audio",
)
print(command)
os.system(command)
files_to_clean.append(base_filename + "/" + "audio.mpd")
os.chdir("..")
def merge_mpds(mpd_file_name):
root = None
for mpd in files_to_clean:
if not root:
root = minidom.parse(mpd).documentElement
continue
period_element = root.childNodes[3]
current_mpd_root = minidom.parse(mpd).documentElement
adaption_set = current_mpd_root.childNodes[3].childNodes[1]
period_element.appendChild(adaption_set)
with open(base_filename + "/" + mpd_file_name, "w") as f:
f.write(root.toxml())
if len(sys.argv) == 1:
print("Enter the filename")
else:
filename = sys.argv[1]
if " " in filename:
new_name = filename.replace(" ", "-")
os.rename(filename, new_name)
filename = new_name
mpd_file_name = filename.replace(".mp4", ".mpd")
base_filename = filename.split(".")[0]
# create output file directory
try:
os.mkdir(base_filename)
except FileExistsError:
pass
create_multiple_bitrate_versions(filename)
create_multiple_segments(filename)
merge_mpds(mpd_file_name)
# cleanup
for file_name in files_to_clean:
print("rm " + file_name)
os.remove(file_name)