-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4mark.py
executable file
·52 lines (40 loc) · 1.24 KB
/
mp4mark.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
#!/usr/bin/env python2
import os
import sys
import re
import glob
from subprocess import call, Popen, PIPE
import json
import mp4select
import xmpmarkers
import ffmeta
files = []
for x in sys.argv[1:]:
files += glob.glob(x) or ([x] if os.path.exists(x) else [])
#import pdb; pdb.set_trace()
base = None
for vid in files:
m = re.match(r'(.*)[_-]\d+p[_-]ame?\.mp4$', vid)
if not m: continue
base = m.group(1)
if base is None:
base = re.sub(r'(.*)-ame\.mp4$', r'\1', files[0])
jsonfile = base + "-chapters.json"
ffmetafile = base + "-chapters.ffmeta"
vtt = base + "-chapters.vtt"
jumplist = base + "-chapters.txt"
videobuf = mp4select.FileBuffer(vid)
chunks = list(mp4select.select("uuid/+16", videobuf))
if len(chunks) == 0:
sys.exit(-1)
(xmpchunk,) = chunks
xmpdata = xmpmarkers.extract(xmpchunk)
chapters = ffmeta.chapters_fix(xmpdata)
json.dump(obj=xmpdata, fp=open(jsonfile, 'w'), sort_keys=True, indent=1)
ffmeta.write_ffdata(chapters, open(ffmetafile, 'w'))
ffmeta.write_webvtt(chapters, open(vtt, 'w'))
ffmeta.write_jumplist(chapters, open(jumplist, 'w'))
for invid in files:
outvid = invid.replace('-ame', '')
assert not os.path.exists(outvid)
call(['ffmpeg', '-i', invid, '-i', ffmetafile, '-c', 'copy', '-movflags', 'faststart', outvid])