Skip to content

Instantly share code, notes, and snippets.

@alastairparagas
Created September 11, 2019 19:47
Show Gist options
  • Select an option

  • Save alastairparagas/25c2d98830a760188f083b2be1f6a421 to your computer and use it in GitHub Desktop.

Select an option

Save alastairparagas/25c2d98830a760188f083b2be1f6a421 to your computer and use it in GitHub Desktop.
MP4 to MP3 file stitcher
import os
import re
import subprocess
DIRECTORY = "/Users/aparagas/Downloads/td_friends"
os.chdir(DIRECTORY)
# Ensure all MP3 files are deleted
for file in os.listdir(os.getcwd()):
if ".mp3" in file:
os.remove(file)
# Ensure MP4 files formatted nicely, convert to MP3 files
for file in os.listdir(os.getcwd()):
if not os.path.isfile(file):
continue
if ".mp4" not in file:
continue
file_name_simple = re.search(r'\d+', file)
if file_name_simple is None:
continue
file_name_simple = file_name_simple.group()
os.rename(
file,
"{name}.mp4".format(name=file_name_simple)
)
subprocess.call("ffmpeg -i {name}.mp4 {name}.mp3".format(
name=file_name_simple
), shell=True)
# Stitch MP3 files together to 1 MP3 file
subprocess.call(
"ffmpeg -i \"concat:{files}\" output.mp3".format(
files="|".join(
sorted([
file
for file in os.listdir(os.getcwd())
if os.path.isfile(file)
if ".mp3" in file
], key=lambda x: int(x.split(".mp3", 1)[0]))
)
),
shell=True
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment