FFMPEG:剪切最后10秒的wave/mp3

3
我正在使用ffmpeg将我的文件从wave转换为mp3。但是出于反盗版的考虑,为了一个新服务,我需要剪辑掉一些歌曲的最后10秒,无论它们有多长。我只找到了关于在已知音轨长度时如何执行此操作的信息,但对于这种情况,我需要自动完成。
有人知道应该使用哪个命令吗?如果能够在最后5秒淡出效果会更好!
1个回答

4

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"

淡出的命令是:```fade-out```。
ffmpeg -i audio.mp3 -af "afade=t=out:st=65:d=5" test.mp3
  • t: 类型(输入|输出)
  • st: 开始时间
  • d: 持续时间

自动化实现

for i in *wav;do python cut_end.py "$i";done

你可以使用连接符 (cutter->fader) 来实现你想要的功能。
祝好。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接