-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideoEncoding.py
104 lines (74 loc) · 4.21 KB
/
videoEncoding.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
import os
import argparse
def createChunks (video, fps, segLen, b360, b480, b720, b1080, bAud, bAr, bAc):
class color:
RED = '\033[91m'
END = '\033[0m'
ResolutionHeightList = [360, 480, 720, 1080]
VideoBitRates = []
VideoBitRates.append(b360 * 1000)
VideoBitRates.append(b480 * 1000)
VideoBitRates.append(b720 * 1000)
VideoBitRates.append(b1080 * 1000)
segLen = float(segLen) * 1000
for ResIndex in range (len(ResolutionHeightList)):
print("{}Starting video enconding for {}p{}".format(color.RED, int(ResolutionHeightList[ResIndex]), color.END))
os.system ('ffmpeg -y -i {} -c:v libx264 \
-r {} -x264opts \'keyint={}:min-keyint={}:no-scenecut\' \
-vf scale=-2:{} -b:v {}k -maxrate {}k \
-movflags faststart -bufsize {}k \
-profile:v main -preset fast -an \"video_intermed_{}p_{}fps.mp4\"'.
format(video, fps, fps * 2, fps * 2, int(ResolutionHeightList[ResIndex]),
float(VideoBitRates[ResIndex]), float(VideoBitRates[ResIndex]), float(VideoBitRates[ResIndex]) * 2,
int(ResolutionHeightList[ResIndex]), fps))
print ("{}Finished video enconding for {}p{}".format(color.RED, int(ResolutionHeightList[ResIndex]), color.END))
print ("{}Starting audio enconding{}".format(color.RED, color.END))
os.system ('ffmpeg -y -i {} -map 0:1 -vn -c:a aac -b:a {}k -ar {}k -ac {} audio{}.m4a'
.format(video, bAud, bAr, bAc, fps))
print ("{}Finished audio enconding{}".format(color.RED, color.END))
print ("{}Starting dashing{}".format(color.RED, color.END))
os.system ("MP4Box -dash {} -frag {} -rap \
-segment-name 'segment_$RepresentationID$_' -fps {} \
video_intermed_{}p_{}fps.mp4#video:id=360p \
video_intermed_{}p_{}fps.mp4#video:id=480p \
video_intermed_{}p_{}fps.mp4#video:id=720p \
video_intermed_{}p_{}fps.mp4#video:id=1080p \
audio{}.m4a#audio:id=Audio:role=main \
-out manifest.mpd".
format(segLen, segLen, fps, ResolutionHeightList[0], fps, ResolutionHeightList[1], fps, ResolutionHeightList[2], fps, ResolutionHeightList[3], fps, fps))
print ("{}Finished dashing{}".format(color.RED, color.END))
os.system ("mkdir encodedVideo encodedVideo/360p encodedVideo/480p encodedVideo/720p encodedVideo/1080p encodedVideo/Audio")
os.system ("mv *.mpd manifest_set1_init.mp4 segment_Audio_.mp4 encodedVideo/")
os.system ("mv *360p*.m4s encodedVideo/360p/")
os.system ("mv *480p*.m4s encodedVideo/480p/")
os.system ("mv *720p*.m4s encodedVideo/720p/")
os.system ("mv *1080p*.m4s encodedVideo/1080p/")
os.system ("mv *Audio*.m4s encodedVideo/Audio/")
with open('encodedVideo/manifest.mpd', 'r') as MPDfile:
fileData = MPDfile.read()
fileData = fileData.replace('segment_360', '360p/segment_360')
fileData = fileData.replace('segment_480', '480p/segment_480')
fileData = fileData.replace('segment_720', '720p/segment_720')
fileData = fileData.replace('segment_1080', '1080p/segment_1080')
fileData = fileData.replace('segment_Audio', 'Audio/segment_Audio')
for ResIndex in range (len(ResolutionHeightList)):
os.system ("rm video_intermed_{}p_{}fps.mp4".format(ResolutionHeightList[ResIndex], fps))
os.system ("rm audio{}.m4a".format(fps))
with open('encodedVideo/manifest.mpd', 'w') as MPDfile:
MPDfile.write(fileData)
def main ():
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--video", help="The video that will be encoded (in MP4 format)")
parser.add_argument("-fps", "--fps", help="The desired video FPS")
parser.add_argument("-segLen", "--segLen", help="DASH segment length")
parser.add_argument("-b360", "--b360", help="Video bitrate (in Mbps) for 360p")
parser.add_argument("-b480", "--b480", help="Video bitrate (in Mbps) for 480p")
parser.add_argument("-b720", "--b720", help="Video bitrate (in Mbps) for 720p")
parser.add_argument("-b1080", "--b1080", help="Video bitrate (in Mbps) for 1080p")
parser.add_argument("-bAud", "--bAud", help="Audio bit rate (in kbps)")
parser.add_argument("-bAr", "--bAr", help="Audio sample rate (in kHz)")
parser.add_argument("-bAc", "--bAc", help="Number of audio channels")
args = parser.parse_args()
createChunks (args.video, args.fps, args.segLen, float(args.b360), float(args.b480), float(args.b720), float(args.b1080), float(args.bAud), float(args.bAr), float(args.bAc))
if __name__ == '__main__':
main()