Skip to content

Instantly share code, notes, and snippets.

@andreanidouglas
Forked from bancek/cue_to_mp3.py
Last active October 30, 2017 11:08
Show Gist options
  • Save andreanidouglas/a093e630cc9933a529f50c7e9cd37f2d to your computer and use it in GitHub Desktop.
Save andreanidouglas/a093e630cc9933a529f50c7e9cd37f2d to your computer and use it in GitHub Desktop.
CUE splitter using ffmpeg (to mp3)
#!/usr/bin/python2
import os
def generate_script(cue_file, file_path, file_name):
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']
print 'mkdir -p "/mnt/Media/Musics/%s/%s"' %( general['artist'].replace('/',''), general['album'].replace('/',''))
for track in tracks:
metadata = {
'artist': track['artist'].replace('/',''),
'title': track['title'],
'album': track['album'],
'track': str(track['track']) + '/' + str(len(tracks))
}
if 'date' in track:
metadata['date'] = track['date']
cmd = 'ffmpeg'
cmd += ' -i "%s/%s"' % (file_path, current_file)
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 += ' ' + ' '.join('-metadata %s="%s"' % (k, v) for (k, v) in metadata.items())
cmd += ' -b:a 320k'
cmd += ' "/mnt/Media/Musics/%s/%s/%.2d - %s - %s.mp3"' % (track['artist'].replace('/',''), general['album'].replace('/',''), track['track'], track['artist'].replace('/',''), track['title'].replace('/',''))
print cmd
x = open('albuns3.txt', 'r')
for line in x:
line = line.strip()
base_dir = '/mnt/Media/FLAC_MUSIC'
dir_name = '%s/%s' %(base_dir, os.path.dirname(line))
file_name = os.path.basename(line)
cue_file = '%s/%s' %(dir_name, file_name)
generate_script(cue_file, dir_name, file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment