forked from beefoo/media-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine_media.py
63 lines (51 loc) · 1.86 KB
/
combine_media.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
# -*- coding: utf-8 -*-
import argparse
import os
from pprint import pprint
import subprocess
import sys
# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="MANIFEST_FILE", default="path/to/ia_fedflixnara_dev.txt", help="Input text file")
parser.add_argument('-dir', dest="MEDIA_DIRECTORY", default="output/", help="Directory with media")
parser.add_argument('-out', dest="OUTPUT_FILE", default="output/ia_fedflixnara_dev.mp4", help="Media output file")
a = parser.parse_args()
dirname = os.path.dirname(a.MANIFEST_FILE)
basename = os.path.basename(a.MANIFEST_FILE)
if not os.path.isfile(a.MANIFEST_FILE):
print("Could not find file %s" % a.MANIFEST_FILE)
sys.exit()
manifestLines = []
with open(a.MANIFEST_FILE) as f:
manifestLines = [l.strip() for l in f.readlines()]
if len(manifestLines) <= 0:
print("Could not find instructions in %s" % a.MANIFEST_FILE)
sys.exit()
# insert media dir
for i, line in enumerate(manifestLines):
if line.startswith("file"):
insertPosition = 5
if line[insertPosition] in ['"', "'"]:
insertPosition = 6
newLine = line[:insertPosition] + a.MEDIA_DIRECTORY + line[insertPosition:]
manifestLines[i] = newLine
# Write to temporary file
tmpFilename = "tmp_" + basename
with open(tmpFilename, 'w') as f:
for line in manifestLines:
f.write("%s\n" % line)
# for more options: https://ffmpeg.org/ffmpeg-formats.html#concat
# https://trac.ffmpeg.org/wiki/Concatenate
# ffmpeg -f concat -safe 0 -i mylist.txt -c copy output
command = ['ffmpeg',
'-y',
'-f', 'concat',
'-safe', '0',
'-i', tmpFilename,
'-c', 'copy', # https://ffmpeg.org/ffmpeg.html#Stream-copy
a.OUTPUT_FILE]
print(" ".join(command))
finished = subprocess.check_call(command)
# delete temp file
os.remove(tmpFilename)
print("Done.")