Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a video file:

screencast.mpg (100sec)

And several audio files(file name indicate offset):

10.wav (10sec)
30.wav (5sec)
45.wav (8sec)

How can I combine those files together:

A:    .....     ...     ....

I want to write a script to do this job automatically.
I've tried ffmpeg -itsoffset option, but failed.

share|improve this question
up vote 1 down vote accepted

The following code assumes that your .wav files are 4800 Hz and stereo.
It builds segments of silence, based on the above values.
It produces a single .wav. which can be muxed with the video.


rawSilence() {
  local nrSeconds=$1
  local sampleRate=$2
  local nrChannels=${3:-2}
  ffmpeg -acodec pcm_s16le \
         -ar $sampleRate \
         -ac $nrChannels \
         -f s16le \
         -i <(dd if=/dev/zero bs=$((sampleRate*2*nrChannels)) count=$nrSeconds) \
         -f s16le -

  rawSilence 10 $sampleRate $nrChannels 
  cat "10.wav" |ffmpeg -f wav -i - -f s16le -
  rawSilence 10 $sampleRate $nrChannels
  cat "30.wav" |ffmpeg -f wav -i - -f s16le -
  rawSilence 10 $sampleRate $nrChannels
  cat "45.wav" |ffmpeg -f wav -i - -f s16le -
  rawSilence 47 $sampleRate $nrChannels
} |
  ffmpeg -acodec pcm_s16le \
         -ar $sampleRate \
         -ac $nrChannels \
         -f s16le \
         -i - \
         -f wav -y 100sec.wav

To mux the video and audio together, this or some encoding variant should do it:

ffmpeg -i "100sec.mpg" \
       -i "100sec.wav" \
          -vcodec copy \
 -y "100sec.muxed.mpg"
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .