Created
December 10, 2015 20:06
-
-
Save aheadley/a8a36ed4c19729b12135 to your computer and use it in GitHub Desktop.
Script to split a youtube video audio file into separate tracks
This file contains hidden or 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
#!/bin/env python3 | |
import sys | |
import re | |
import subprocess | |
TRACK_LINE_RE = re.compile(r'^(?P<time>(?:\d+:)?\d+:\d+) Track (?P<idx>\d+)-(?P<title>.*)$') | |
def time_to_seconds(time): | |
time_spec = [1, 1 * 60, 1 * 60 * 60] | |
time_parts = [int(i) for i in time.split(':')[::-1]] | |
return sum(i[0] * i[1] for i in zip(time_parts, time_spec)) | |
if __name__ == '__main__': | |
import sys | |
cue_fn = sys.argv[1] | |
audio_fn = sys.argv[2] | |
with open(cue_fn) as cue_f: | |
tracks = [TRACK_LINE_RE.match(cue_line.strip()).groupdict() for cue_line in cue_f if cue_line.strip()] | |
for i, track in enumerate(tracks): | |
print(track) | |
time = time_to_seconds(track['time']) | |
idx = int(track['idx']) | |
title = track['title'] | |
out_fn = u'{idx:02d} - {title}.flac'.format(idx=idx, title=title) | |
if i+1 == len(tracks): | |
subprocess.call( | |
['ffmpeg', '-ss', str(time), '-i', audio_fn, | |
out_fn], | |
stdout=sys.stdout) | |
else: | |
subprocess.call( | |
['ffmpeg', '-ss', str(time), '-i', audio_fn, | |
'-t', str(time_to_seconds(tracks[i+1]['time']) - time), | |
out_fn], | |
stdout=sys.stdout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment