Skip to content

Instantly share code, notes, and snippets.

@axsddlr
Forked from kiwhite/avi2mp4.sh
Last active August 23, 2016 23:47
Show Gist options
  • Save axsddlr/3c8a9edc434a681e6b541b85df8ac4e3 to your computer and use it in GitHub Desktop.
Save axsddlr/3c8a9edc434a681e6b541b85df8ac4e3 to your computer and use it in GitHub Desktop.
Convert AVI to MP4 with ffmpeg
#! /bin/bash
timestamp=$( date +"%Y%m%d" )
base=${file%.*}
a1=$(ffprobe -i $VID 2>&1 |grep 'Stream #0:1'.| sed 's/\s.*$//')
a2=$(ffprobe -i $VID 2>&1 |grep 'Stream #0:2'.| sed 's/\s.*$//')
dir=$(pwd)
for VID in /media/gp/*.avi
do
#create folder
echo "Found ${VID}... Starting ffmpeg."
mkdir `basename ${VID%%.*}`
chmod -R 0777 `basename ${VID%%.*}`
cp $VID `basename ${VID%%.*}`
cd `basename ${VID%%.*}`
#check if video has two audio streams
if [ "$a1"="Stream #0:1: Audio: pcm_" ] && [ "$a2"="Stream #0:2: Audio: pcm_" ]
then
ffmpeg -i $VID -map 0:1 -c copy audio0.wav -map 0:2 -c copy audio1.wav
ffmpeg -i audio1.wav -vn -ss 00:00:00 -t 00:00:01 noiseaud.wav
sox noiseaud.wav -n noiseprof noise.prof
sox audio1.wav audio1_clean.wav noisered noise.prof 0.21
fi
if [ -f /media/gp/`basename ${VID%%.*}`/audio0.wav ] && [ -f /media/gp/`basename ${VID%%.*}`/audio1.wav ]
then
echo "Two audio files present"
ffmpeg -i $VID -crf 17 -preset veryfast -codec:v libx264 -threads 2 -strict experimental -b:a 256k -ac 2 -an `basename $VID avi`'mp4'
rm *.prof noiseaud.wav audio1.wav *.avi
else
echo "One audio file present"
ffmpeg -i $VID -crf 17 -preset veryfast -codec:v libx264 -threads 2 -strict experimental -b:a 256k -ac 2 `basename $VID avi`'mp4'
fi
done
#!/bin/bash
pipe=/tmp/ffmpeg
trap "rm -f $pipe" EXIT
# creating the FIFO
[[ -p $pipe ]] || mkfifo $pipe
while true; do
# can't just use "while read line" if we
# want this script to continue running.
read line < $pipe
# now implementing a bit of security,
# feel free to improve it.
# we ensure that the command is a ffmpeg one.
[[ $line =~ ^ffmpeg ]] && bash <<< "$line"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment