-
-
Save sashagoebbels/6d0537ae14731f88ef103709c21fc726 to your computer and use it in GitHub Desktop.
Quick and dirty mp4 file cutting by cue sheet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
"""Quick and dirty mp4 file cutting by cue sheet. | |
The cue sheet shall look somewhat like this:: | |
PERFORMER "Calexico & ORF Radio-Symphonieorchester" | |
TITLE "FM4 Radiosession mit Calexico" | |
FILE "mp4_rs_calexico_final_2_q4a_200152.mp4" MP3 | |
TRACK 01 AUDIO | |
PERFORMER "Calexico" | |
TITLE "Intro" | |
INDEX 01 00:00:00 | |
Uses MP4Box (http://gpac.wp.mines-telecom.fr/mp4box/) for the cutting. Since the | |
'-out' argument seems to be ignored with '-splitx' it renames the output files | |
using 'mv' according to track number, artist and title in the cue sheet. | |
I did not find a way to cut from a tiem to the end of the file, so you need to | |
set the total length of your input manually! | |
""" | |
cuefile = "mp4_rs_calexico_final_2_q4a_200152.cue" | |
mp4file = "mp4_rs_calexico_final_2_q4a_200152.mp4" | |
mp4length = 4250 | |
from subprocess import call | |
def parse_cuefile(cuefile): | |
cue = {'TRACKS': []} | |
with open(cuefile, 'r') as fh: | |
for line in fh: | |
_ = line.strip().split(" ") | |
key = _[0] | |
values = _[1:] | |
#print key, values | |
if key in ['PERFORMER', 'TITLE', 'FILE'] and not key in cue.keys(): | |
cue[key] = " ".join(values).replace('"', '') | |
continue | |
if key in ['FILE']: | |
cue[key] = values[0].replace('"', '') | |
cue['TYPE'] = values[1].replace('"', '') | |
continue | |
if key == 'TRACK': | |
tmp = {} | |
tmp['TRACK'] = values[0] | |
continue | |
if key == 'PERFORMER': | |
tmp['ARTIST'] = values[0].replace('"', '') | |
continue | |
if key == 'TITLE': | |
tmp['TITLE'] = " ".join(values).replace('"', '') | |
continue | |
if key == 'INDEX': | |
tmp['TSTAMP'] = values[1] | |
m, s, _ = values[1].split(":") | |
tmp['TSTAMP_SEC'] = int(m)*60+int(s) | |
cue['TRACKS'].append(tmp) | |
continue | |
return cue | |
def call_mp4box(track, artist, title, start, end, mp4file): | |
call('MP4Box -splitx {}:{} {}'.format(start, end, mp4file), shell=True) | |
_ = mp4file.split(".") | |
out = '{}_{}_{}.{}'.format(''.join(_[:-1]), start, end, _[-1]) | |
call('mv {} "{} - {} - {}.mp4"'.format(out, track, artist, title), | |
shell=True) | |
def cut_mp4file(cue, mp4file, length): | |
for n in range(0, len(cue['TRACKS'])): | |
if n < len(cue['TRACKS'])-1: | |
call_mp4box(cue['TRACKS'][n]['TRACK'], cue['TRACKS'][n]['ARTIST'], | |
cue['TRACKS'][n]['TITLE'], cue['TRACKS'][n]['TSTAMP_SEC'], | |
cue['TRACKS'][n+1]['TSTAMP_SEC'], mp4file) | |
else: | |
call_mp4box(cue['TRACKS'][n]['TRACK'], cue['TRACKS'][n]['ARTIST'], | |
cue['TRACKS'][n]['TITLE'], cue['TRACKS'][n]['TSTAMP_SEC'], | |
length, mp4file) | |
cut_mp4file(parse_cuefile(cuefile), mp4file, mp4length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment