Skip to content

Instantly share code, notes, and snippets.

@titipata
Last active May 4, 2018 09:02
Show Gist options
  • Save titipata/a5aa35c3337292ace0e331c09c28c061 to your computer and use it in GitHub Desktop.
Save titipata/a5aa35c3337292ace0e331c09c28c061 to your computer and use it in GitHub Desktop.
Convert MP4 to Spectogram

Convert files in MP4 folder to FLAC

Scipt to convert

import os
from glob import glob
import subprocess

MP4_PATH = '~/Desktop/notebooks/mp4/*.m4a' # path to all mp4 files
OUTPUT_PATH = '~/Desktop/notebooks/flac/' # any path to save as flac file
OUTPUT_SPECTOGRAM_PATH = '~/Desktop/notebooks/spectogram/'

mp4_paths = glob(MP4_PATH)
if not os.path.isdir(OUTPUT_PATH):
    os.mkdir(OUTPUT_PATH)
if not os.path.isdir(OUTPUT_SPECTOGRAM_PATH):
    os.mkdir(OUTPUT_SPECTOGRAM_PATH)

for mp4_path in mp4_paths:
    file_name = os.path.splitext(os.path.basename(mp4_path))[0]
    file_name_flac = file_name + '.flac'
    file_name_png = file_name + '.png'
    subprocess.call(['ffmpeg', '-i', mp4_path, '-acodec', 
                     'flac', '-ab', '128k', '-ar', '44100', 
                     '-ac', '1', '-f', 'flac', '-t', '60', 
                     os.path.join(OUTPUT_PATH, file_name_flac)])
    subprocess.call(['sox', os.path.join(OUTPUT_PATH, file_name_flac), 
                     '-n', 'spectrogram', 
                     '-Y', '200', '-X', '50', '-m', 
                     '-r', '-o', os.path.join(OUTPUT_SPECTOGRAM_PATH, file_name_png)])

See explanation on -X and -Y here. Basically, 50 means 50 pixels/second (20 ms bin size) and 200 means it will select 128 as frequency bin since it's the highest 2^n that before 200.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment