h264 mkv to mp4 without transcoding

Park

Assuming you have PS3 and some media-server (UPnP/DLNA which are essentially the same, or at least we can call these terms identical for the sake of clarity).

Assuming you are more or less copyleft (with very marginal presence of copyright). Assuming you are in need of converting matroska mkv files to mpeg4 h264 and you don’t really care about subtitles but you do care about avoiding re-coding and retaining 5.1 soundtrack.
The idea is taken from this post. The script works on mac but should also do on any other *nix system.
Software needed:
ffmpeg
mpeg4ip
mkvtoolnix

Pro: No video conversion (and if you are in luck no audio conversion as well)

Con: No subtitles!

for i in `ls *.mkv`; doINPUT_NAME=`echo "${i}" | perl -pi -e 's/(^.*)(\..*$)/\1/g'`info=`/usr/local/bin/ffmpeg -i "${i}" 2>&1`FPS=`echo "${info}" | grep Video: | gawk -F\:\  '{ print $3 }' | gawk -F, '{ print $4 }' | cut -c 2-6`CODEC=`echo "${info}" | grep Audio: | gawk -F\:\  '{ print $3 }' | gawk -F, '{ print $1 }'`CHANNELS=`echo "${info}" | grep Audio: | gawk -F\:\  '{ print $3 }' | gawk -F, '{ print $3 }'`

if [[ "$CHANNELS" == *5:1* ]]; then     
CHANNELS=5:1
else     
if [[ "$CHANNELS" == *stereo* ]]; then             
CHANNELS=stereo     
else             
CHANNELS=mono     
fi
fi

case "$CHANNELS" in     
stereo)             CHANNELS="2"     ;;     
mono)             CHANNELS="1"     ;;     
5:1)             CHANNELS="6"     ;;
esac

#clenase leftovers if any    
rm video.h264    
rm audio.aac
#extract data
#Extract Audio
if [[ "$CODEC" == mpeg4aac ]]; then
mkvextract tracks "${i}" 2:audio.aac
else
ffmpeg -i "${i}" -vn -acodec libfaac -ac ${CHANNELS} -sameq -y audio.aac
fi
#Extract Video
mkvextract tracks "${i}" 1:video.h264
#change the profile type
echo \) | dd bs=1 conv=notrunc of=video.h264 seek=7 count=1
#create resulting file
#delete mp4 file if exists (in case we run the converter for the second time)
if [ -f "${INPUT_NAME}".mp4 ]; then 
rm -f "${INPUT_NAME}".mp4; 
fi
mp4creator -create=video.h264 -rate=$FPS "${INPUT_NAME}".mp4mp4creator -hint=1 "${INPUT_NAME}".mp4
mp4creator -c audio.aac -interleave -optimize "${INPUT_NAME}".mp4
#cleanse
rm video.h264
rm audio.aac
done

Leave a Reply