Skip to content

Instantly share code, notes, and snippets.

@meoow meoow/ffaac.py
Created Oct 24, 2014

Embed
What would you like to do?
Convert all FFmpeg supported media file to high quality AAC file (need libfdk_aac encoder)
#!/usr/bin/env python2.7
from optparse import OptionParser
from subprocess import call
from os.path import splitext
import sys, os
def ffaac(filename, opts):
cmd = ['ffmpeg', '-hide_banner', '-y', '-i', filename,
'-c:a', 'libfdk_aac']
profile = {'lc':'aac_low', 'he':'aac_he', 'he2':'aac_he_v2',
'ld':'aac_ld', 'eld':'aac_eld'}
if not opts.br and not opts.vbr:
cmd.extend(['-b:a', '256k'])
if opts.br:
cmd.extend(['-b:a', str(opts.br)+'k'])
if opts.br >= 256:
cmd.extend(['-cutoff', '20000']) #20khz is the upper limit
elif opts.br >= 192:
cmd.extend(['-cutoff', '18000'])
if opts.vbr:
cmd.extend(['-vbr', opts.vbr])
if opts.ar:
cmd.extend(['-ar', opts.ar])
if opts.ch:
cmd.extend(['-ac', opts.ch])
cmd.extend(['-profile:a', profile[opts.prof]])
if opts.prof == 'eld' and opts.sbr:
cmd.extend(['-eld_sbr', '1'])
cmd.extend([
'-write_id3v2', '1',
'-write_id3v1', '1',
'-id3v2_version', '3',
'-f', 'adts'])
if filename.lower().endswith('.aac'):
return
outfile = splitext(filename)[0] + '.aac'
cmd.append(outfile)
print cmd
return call(cmd)
if __name__=='__main__':
parser = OptionParser()
parser.add_option('-b', metavar='NUM', type=int, dest='br')
parser.add_option('-r', metavar='NUM', choices=['8000','11025','16000','22050','32000', '44100', '48000'], dest='ar', help='8000/11025/16000/22050/32000/44100/48000')
parser.add_option('-c', metavar='NUM', choices=['1','2'],dest='ch', help='1/2')
parser.add_option('-p', metavar='TYPE',choices=['lc', 'he', 'he2', 'ld', 'eld'], default='lc', dest='prof', help='lc/he/he2/ld/eld')
parser.add_option('-v', metavar='QUAL', choices=['1', '2', '3', '4', '5'], dest='vbr', help='1/2/3/4/5')
parser.add_option('-s', action='store_true', dest='sbr', help='SBR for ELD')
parser.add_option('-d', action='store_true', dest='delete')
opts, args = parser.parse_args()
for f in args:
if ffaac(f, opts) == 0 and opts.delete :
try:
os.remove(f)
except OSError:
pass
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.