Skip to content

Instantly share code, notes, and snippets.

@saksmt
Forked from bancek/cue_to_mp3.py
Last active November 15, 2016 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saksmt/a9440624633f08ff91abb380771c06d0 to your computer and use it in GitHub Desktop.
Save saksmt/a9440624633f08ff91abb380771c06d0 to your computer and use it in GitHub Desktop.
CUE splitter using ffmpeg (to source format)
#!/usr/bin/env python2
# Shit code.. TODO: rewrite
import sys
import os
import os.path
import subprocess
import itertools
args = sys.argv[1:]
cue_file = " ".join(args)
if cue_file == "":
print "ERROR: NO INPUT FILE PRESENT!"
sys.exit(-1)
d = open(cue_file).read().splitlines()
general = {}
tracks = []
current_file = None
for line in d:
if line.startswith('REM GENRE '):
general['genre'] = ' '.join(line.split(' ')[2:])
if line.startswith('REM DATE '):
general['date'] = ' '.join(line.split(' ')[2:])
if line.startswith('PERFORMER '):
general['artist'] = ' '.join(line.split(' ')[1:]).replace('"', '')
if line.startswith('TITLE '):
general['album'] = ' '.join(line.split(' ')[1:]).replace('"', '')
if line.startswith('FILE '):
current_file = ' '.join(line.split(' ')[1:-1]).replace('"', '')
if line.startswith(' TRACK '):
track = general.copy()
track['track'] = int(line.strip().split(' ')[1], 10)
tracks.append(track)
if line.startswith(' TITLE '):
tracks[-1]['title'] = ' '.join(line.strip().split(' ')[1:]).replace('"', '')
if line.startswith(' PERFORMER '):
tracks[-1]['artist'] = ' '.join(line.strip().split(' ')[1:]).replace('"', '')
if line.startswith(' INDEX 01 '):
t = map(int, ' '.join(line.strip().split(' ')[2:]).replace('"', '').split(':'))
tracks[-1]['start'] = 60 * t[0] + t[1] + t[2] / 100.0
for i in range(len(tracks)):
if i != len(tracks) - 1:
tracks[i]['duration'] = tracks[i + 1]['start'] - tracks[i]['start']
cue_file = os.path.abspath(cue_file)
track_dir = os.path.dirname(cue_file)
os.chdir(track_dir)
current_file = '%s/%s' % (track_dir, current_file)
extension = current_file.split('.')[-1:][0]
try:
os.mkdir('splitted')
except:
pass
for track in tracks:
metadata = {
'artist': track['artist'],
'title': track['title'],
'album': track['album'],
'track': str(track['track']) + '/' + str(len(tracks))
}
if 'genre' in track:
metadata['genre'] = track['genre']
if 'date' in track:
metadata['date'] = track['date']
cmd = ['ffmpeg']
cmd += ['-i', current_file]
cmd += ['-c', 'copy']
cmd += ['-ss', '%.2d:%.2d:%.2d' % (track['start'] / 60 / 60, track['start'] / 60 % 60, int(track['start'] % 60))]
if 'duration' in track:
cmd += ['-t', '%.2d:%.2d:%.2d' % (track['duration'] / 60 / 60, track['duration'] / 60 % 60, int(track['duration'] % 60))]
cmd += list(itertools.chain.from_iterable(['-metadata', '%s="%s"' % (k, v)] for (k, v) in metadata.items()))
cmd += ['splitted/%.2d - %s.%s' % (track['track'], track['title'], extension)]
print 'Executing %s' % ' '.join(cmd)
subprocess.call(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment