Skip to content

Instantly share code, notes, and snippets.

@jarun

jarun/split.py

Created May 6, 2019
Embed
What would you like to do?
Split a music track into specified sub-tracks using ffmpeg
#!/usr/bin/env python3
'''
Description: split a music track into specified sub-tracks using ffmpeg
target files are saved as variable bit-rate mp3 (lossless)
Usage: split <original_track> <track_list>
'''
import shlex
import subprocess
import sys
def main():
# check command line for original file and track list file
if len(sys.argv) != 3:
print("usage: split <original_track> <track_list>")
exit(1)
# record command line args
original_track = sys.argv[1]
track_list = sys.argv[2]
# create a template of the ffmpeg call in advance
cmd_string = 'ffmpeg -i {tr} -vn -sn -acodec libmp3lame -q:a 2 -ss {st} -to {en} {nm}.mp3'
# read each line of the track list and split into start, end, name
with open(track_list, 'r') as f:
for line in f:
# skip comment and empty lines
if line.startswith('#') or len(line) <= 1:
continue
# create command string for a given track
start, end, name = line.strip().split(maxsplit=2)
command = cmd_string.format(tr=shlex.quote(original_track), st=start, en=end,
nm=shlex.quote(name))
# use subprocess to execute the command in the shell
subprocess.call(command, shell=True)
return None
if __name__ == '__main__':
main()
@jarun

This comment has been minimized.

Copy link
Owner Author

@jarun jarun commented May 6, 2019

Sample track list file.

00:00:00 00:03:38 01. Another One Bites the Dust
00:03:38 00:09:37 02. Bohemian Rhapsody
00:09:37 00:12:22 03. Crazy Little Thing Called Love
00:12:22 00:16:04 04. Don t Stop Me Now
00:16:04 00:19:52 05. I Want to Break Free
00:19:52 00:22:54 06. Killer Queen
00:22:54 00:28:46 07. Radio Ga Ga
00:28:46 00:33:45 08. Somebody to Love
00:33:45 00:35:51 09. We Will Rock You
00:35:51 00:38:54 10. We Are the Champions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.