这是我的脚本包装ffmpeg,用于将任何支持的音频格式转换为AAC(使用libmpdk-aac编码器,这是ffmpeg Wiki推荐的aac编码器)。
#!/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,
'-vn', '-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', '320k'])
if opts.br:
cmd.extend(['-b:a', str(opts.br)+'k'])
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(['-f', 'adts'])
if filename.lower().endswith('.aac'):
return
outfile = splitext(filename)[0] + '.aac'
cmd.append(outfile)
return call(cmd)
if __name__=='__main__':
parser = OptionParser(sys.argv[0]+' [OPTIONS] FILE ...')
parser.add_option('-b', metavar='NUM', type=int, dest='br', help='bitrate')
parser.add_option('-r', metavar='NUM', choices=['8000','11025','16000','22050','32000', '44100', '48000'], dest='ar', help='sample rate: 8000/11025/22050/32000/44100/48000')
parser.add_option('-c', metavar='NUM', choices=['1','2'],dest='ch', help='channels: 1/2')
parser.add_option('-p', metavar='TYPE',choices=['lc', 'he', 'he2', 'ld', 'eld'], default='lc', dest='prof', help='profile: lc/he/he2/ld/eld')
parser.add_option('-v', metavar='QUAL', choices=['1', '2', '3', '4', '5'], dest='vbr', help='vbr quality: 1/2/3/4/5')
parser.add_option('-s', action='store_true', dest='sbr', help='use SBR for ELD')
parser.add_option('-d', action='store_true', dest='delete',help='delete source after converting')
opts, args = parser.parse_args()
for f in args:
if ffaac(f, opts) == 0 and opts.delete :
try:
os.remove(f)
except OSError:
pass
命令行帮助消息:
Usage: /Users/leon/bin/ffaac.py [OPTIONS] FILE ...
Options:
-h, --help show this help message and exit
-b NUM bitrate
-r NUM sample rate: 8000/11025/22050/32000/44100/48000
-c NUM channels: 1/2
-p TYPE profile: lc/he/he2/ld/eld
-v QUAL vbr quality: 1/2/3/4/5
-s use SBR for ELD
-d delete source after converting