我正在使用ffmpeg将我的文件从wave转换为mp3。但是出于反盗版的考虑,为了一个新服务,我需要剪辑掉一些歌曲的最后10秒,无论它们有多长。我只找到了关于在已知音轨长度时如何执行此操作的信息,但对于这种情况,我需要自动完成。
有人知道应该使用哪个命令吗?如果能够在最后5秒淡出效果会更好!
有人知道应该使用哪个命令吗?如果能够在最后5秒淡出效果会更好!
Python是一个功能强大的工具,几乎可以用来做任何事情(在Linux中测试过)
#!/bin/python
from sys import argv
from os import system
from subprocess import Popen, PIPE
ffm = 'ffmpeg -i' # input file
aud = ' -acodec mp3' #add your quality preferences
dur = ' 2>&1 | grep "Duration" | cut -d " " -f 4'
def cutter(inp,t=0):
out = inp[:-5] + '_cut' + inp[-5:]
cut = ' -t %s' % ( duration(inp)-t )
cmd = ffm + inp + aud + cut + out
print cmd; system(cmd)
def fader(inp,t=0):
out = inp[:-5] + '_fade' + inp[-5:]
fad = ' -af "afade=t=out:st=%s:d=%s"' % ( duration(inp)-t, t )
cmd = ffm + inp + fad + out
print cmd; system(cmd)
def duration(inp):
proc = Popen(ffm + inp + dur, shell=True, stdout=PIPE, stderr=PIPE)
out,err = proc.communicate()
h,m,s = [float(x) for x in out[:-2].split(':')]
return (h*60 + m)*60 + s
if __name__ == '__main__':
fname=' "'+argv[1]+'"'
cutter(fname,10)
fader (fname, 5)
# $ python cut_end.py "audio.mp3"
ffmpeg -i audio.mp3 -af "afade=t=out:st=65:d=5" test.mp3
自动化实现
for i in *wav;do python cut_end.py "$i";done