Skip to content

Instantly share code, notes, and snippets.

@treeform
Forked from miohtama/gist:2727891
Created May 18, 2012 22:34
Show Gist options
  • Save treeform/2727938 to your computer and use it in GitHub Desktop.
Save treeform/2727938 to your computer and use it in GitHub Desktop.
Create HTML5 <audio> compatible files out of MP3 using ffmpeg
# -*- coding: utf8 -*-
import os
import subprocess
def create_prelisten_ogg(mp3, ogg):
"""
Run en-code for a single file
Do 48 kbit files for prelisten.
"""
FFMPEG = os.environ['FFMPEG']
cmdline = [ FFMPEG, '-y', '-i', mp3, '-acodec', 'libvorbis', '-ar', '22050', '-ac', '1', '-ab', '48000', ogg ]
return subprocess.call(cmdline)
def create_prelisten_aac(mp3, aac):
"""
Run en-code for a single file
Do 48 kbit files for prelisten.
"""
FFMPEG = os.environ['FFMPEG']
cmdline = [ FFMPEG, '-y', '-i', mp3, '-acodec', 'libfaac', '-ar', '22050', '-ac', '1', '-ab', '48000', aac ]
return subprocess.call(cmdline)
getBrowserAudioFormat : function(url) {
var audio = document.createElement("audio");
var needAAC = audio.canPlayType('audio/mp4; codecs="mp4a.40.5"') !== "";
var needOGG = audio.canPlayType('audio/ogg; codecs="vorbis"') !== "";
if(needOGG) {
url = url.replace(".mp3", ".ogg");
} else if(needAAC){
url = url.replace(".mp3", ".m4a");
} else {
console.error("Could not detect prelisten audio format support");
}
return url;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment