Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python Youtube MP3 Splitter #gistblog #python

Python Youtube MP3 Splitter

A Python program that splits mp3 files from YouTube albums into individual songs.


  • Python 2.7+
  • ffmpeg

Getting Started

You need to have Python 2.7+ and ffmpeg installed.

Useful links:


  1. Install Python and ffmpeg.

  2. Download the album from Youtube as mp3.
    (You could use YouTube-dl or any online service).

youtube-dl --extract-audio --audio-format mp3 --audio-quality 0 <Video-URL>
  1. Download

  2. Copy the timestamps into a .txt file

  3. Run

    python <music file.mp3> <tracklist file.txt>
  4. Enjoy!

import sys
import subprocess
inputfile = sys.argv[1]
codec = '-acodec'
#ffmpeg did not like having '?' in the file name, add any other problematic symbol here.
escape_list = ['?']
def RemoveSymbols(text):
for symbol in escape_list:
text = text.replace(symbol, '')
return text
tracklist = []
class Track:
def __init__(self, timestamp, name):
self.timestamp = timestamp = name
class ExtractTracks:
def __init__(self):
with open(sys.argv[2], "r") as values:
for value in values:
name = ""
timestamp = ""
#split all by spaces.
keyVal = value.split(' ')
#find timestamp
for word in keyVal:
if ':' in word:
timestamp = word
name += word + ' '
tracklist.append(Track(timestamp, name))
def GenerateSplitCommand(start, end, filename):
return ['ffmpeg', '-i', inputfile, '-ss', start, '-to', end, '-c', 'copy', filename+'.mp3', '-v', 'error']
def GetVideoEnd():
ffprobeCommand = [
return subprocess.check_output(ffprobeCommand).strip()
for i in range(0, len(tracklist)):
name = tracklist[i].name.strip()
name = RemoveSymbols(name)
startTime = tracklist[i].timestamp.strip()
if i != (len(tracklist) - 1):
endTime = tracklist[i+1].timestamp.strip() #- startTime
endTime = GetVideoEnd() #- startTime
print('Generating ' + name + ' from ' + startTime + ' to ' + endTime)
command = GenerateSplitCommand(str(startTime), str(endTime), name)
output = subprocess.check_call(command)
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.