Skip to content

Instantly share code, notes, and snippets.

@danpaluska
Created March 24, 2010 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danpaluska/341936 to your computer and use it in GitHub Desktop.
Save danpaluska/341936 to your computer and use it in GitHub Desktop.
Add audio to a movie file. uses sox and ffmpeg. mac/linux
#!/bin/bash
###
## USAGE ./add_audio.sh moviefile.mp4 audiofile.mp3
## this will take snippet from audio file and add it to moviefile.
## it will then trim that snippet from audiofile.mp3
DURATION=`ffmpeg -i $1 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//`
###
# sox inputfilename.mp3 trimmedfilename.mp3 trim starttime duration
# sox foo.wav trimfoo.wav trim 10 10 # gives you 10-20seconds into foo.wav
# 00:00:19.55 HH:MM:SS.xx
echo "duration = ${DURATION}"
length_seconds=`sox --info -D ${2} | cut -d"." -f1`
sox $2 temp_audio.mp3 trim 0 $DURATION
length_snip=`sox --info -D temp_audio.mp3 | cut -d"." -f1`
ffmpeg -sameq -i $1 -i temp_audio.mp3 -ab 192k tempout.mp4
# mv tempout.mp4 $1
let "NEWL=${length_seconds}-${length_snip}"
sox $2 ${2}new.mp3 trim $DURATION $NEWL
mv tempout.mp4 $1
mv ${2}new.mp3 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment