Created
May 6, 2019 15:58
-
-
Save jarun/8c996cf77a4b6462b0426526ef45685d to your computer and use it in GitHub Desktop.
Split a music track into specified sub-tracks using ffmpeg
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/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample track list file.