Morten Hjort
Morten Hjort

Reputation: 1012

FFMPEG: Cut out last 10 seconds of wave/mp3

I'm using ffmpeg to convert my files from wave to mp3. But for a new service I need to cut out the last 10 seconds of some of the songs (for piracy issues), no matter how long they are. I've only found information about doing this when the length of the track has been known, but for this I need to do it automatically.

Does anyone know which command to use? If I can fade-out 5 seconds before that would be optimal!

Upvotes: 3

Views: 2889

Answers (1)

giancarlo colasante
giancarlo colasante

Reputation: 76

python is a powerfull tool for almost everything (tested in 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"

To fade-out the command is

ffmpeg -i audio.mp3 -af "afade=t=out:st=65:d=5" test.mp3
  • t: type (in|out)
  • st: start time
  • d: duration

To automate

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

You can concatenate (cutter->fader) to do what you want.

Regards.

Upvotes: 4

Related Questions