Managing Media Files
Setup
brew install ffmpeg mediainfo mkvtoolnix mp4box
mv SublerCLI /usr/local/bin/SublerCLI
Identifying Problems in Files
mediainfo source.mkv
Convert Audio
Extract AC3 / DTS
ffmpeg -i video.mkv -acodec copy audio.ac3
Convert to AC3
ffmpeg -i audio.dts -acodec ac3 -ab 448k -ac 6 output.ac3
Convert to AAC
ffmpeg -i audio.dts -acodec aac -ab 192k -ac 2 -strict -2 output.m4a
ffmpeg -i audio.dts -acodec libfaac -ab 192k -ac 2 output.m4a
Convert to AAC 5.1
ffmpeg -i audio.dts -acodec aac -ab 1536k -ac 6 -strict -2 output.m4a
ffmpeg -i audio.dts -acodec libfaac -ab 1536k -ac 6 output.m4a
Convert to FLAC
ffmpeg -i audio.dts output.flac
Convert Container
Convert AVI to MKV
Ordering of tracks will change with from file to file.
for video in *.avi; do
filename=$(basename "$video" .avi)
echo "Processing $filename ...";
/Applications/Mkvtoolnix.app/Contents/MacOS/mkvmerge -o "${filename}.mkv" \
--language "0:eng" --track-name "0:XVID" --forced-track "0:no" \
--language "1:eng" --track-name "1:MP3 2.0" --forced-track "1:no" \
--audio-tracks 1 --video-tracks 0 \
--no-subtitles --no-track-tags --no-global-tags --no-chapters \
"${filename}.avi" --track-order "0:0,0:1"
done
Convert MKV to MP4
Alternatives: https://github.com/Bilalh/MKV-Utilities/blob/master/encoding.sh https://github.com/flexiondotorg/MKV-to-MP4/blob/master/MKV-to-MP4.sh
SublerCLI -source source.mkv -dest source.mp4
for video in *.mkv; do
filename=$(basename "$video" .mkv)
echo "Processing $filename ...";
SublerCLI -source "${filename}.mkv" -dest "${filename}.mp4"
mp4box -unhint "${filename}.mp4"
mp4box -hint "${filename}.mp4"
done
ffmpeg -i input.mkv -vcodec copy -acodec copy -scodec copy
ffmpeg -i input.mkv -vcodec copy -acodec aac -ab 192k -ac 2 -strict -2 output.m4a -scodec copy # (2ch)
ffmpeg -i input.mkv -vcodec copy -acodec aac -ab 1536k -ac 6 -strict -2 output.m4a -scodec copy # (5.1ch)
Convert MP4 to MKV
/Applications/Mkvtoolnix.app/Contents/MacOS/mkvmerge -o "destination.mkv" \
--language "0:eng" --track-name "0:6ch AC3" --default-track "0:yes" --forced-track "0:no" --compression "0:none" \
--language "1:eng" --track-name "1:x264" --default-track "1:yes" --forced-track "1:no" --compression "1:none" --display-dimensions "1:1280x720" \
--audio-tracks 0 --video-tracks 1 --no-subtitles --no-track-tags --no-global-tags --no-chapters \
"source.mkv" --track-order "0:1,0:0"
Convert FLV to MP3
The same applies to any supported audio codec in the FLV.
ffmpeg -i source.flv -vn -acodec copy destination.mp3
Convert FLV to MKV/MP4
This assumes that the MKV/MP4 container accepts the audio / video format in the FLV.
Windows
ffmpeg -i source.flv -vcodec copy -acodec copy destination.mkv
ffmpeg -i source.flv -vcodec copy -acodec copy destination.mp4
Fixing MP4 Hinting Metadata
mp4box -unhint source.mp4
mp4box -hint source.mp4
for video in *.mp4; do
filename=$(basename "$video" .mp4)
echo "Processing $filename ...";
mp4box -unhint "${filename}.mp4"
mp4box -hint "${filename}.mp4"
done
Fixing MKV Metadata
Remove Header Compression
Some hardware players have issues with header compression introduced in 4.0.0
/Applications/Mkvtoolnix.app/Contents/MacOS/mkvmerge -o "destination.mkv" \
--no-attachments --compression '-1:none' \
"source.mkv"
Reorder and Correct Tracks
/Applications/Mkvtoolnix.app/Contents/MacOS/mkvmerge -o "destination.mkv" \
--language "0:eng" --track-name "0:6ch AC3" --default-track "0:yes" --forced-track "0:no" --compression "0:none" \
--language "1:eng" --track-name "1:x264" --default-track "1:yes" --forced-track "1:no" --compression "1:none" --display-dimensions "1:1280x720" \
--audio-tracks 0 --video-tracks 1 --no-subtitles --no-track-tags --no-global-tags --no-chapters \
"source.mkv" \
--track-order "0:1,0:0"
Reorder and Correct Tracks in Groups of Files
for video in *.mkv
do
filename=$(basename "$video" .mkv)
audio_track_id=$(/Applications/Mkvtoolnix.app/Contents/MacOS/mkvmerge -i "$video" | grep -m 1 "audio" | cut -d ":" -f 1 | cut -d " " -f 3)
video_track_id=$(/Applications/Mkvtoolnix.app/Contents/MacOS/mkvmerge -i "$video" | grep -m 1 "video" | cut -d ":" -f 1 | cut -d " " -f 3)
# echo " $filename : $audio_track_id : $video_track_id ";
/Applications/Mkvtoolnix.app/Contents/MacOS/mkvmerge -o "${filename}.clean.mkv" \
--language "${audio_track_id}:eng" --track-name "${audio_track_id}:2ch AC3" --default-track "${audio_track_id}:yes" --forced-track "${audio_track_id}:no" --compression "${audio_track_id}:none" \
--language "${video_track_id}:eng" --track-name "${video_track_id}:x264" --default-track "${video_track_id}:yes" --forced-track "${video_track_id}:no" --compression "${video_track_id}:none" --display-dimensions "${video_track_id}:1280x720" \
--audio-tracks $audio_track_id --video-tracks $video_track_id --no-subtitles --no-track-tags --no-global-tags --no-chapters \
"${filename}.mkv" \
--language "0:eng" --track-name "0:SRT" --forced-track "0:no" --subtitle-tracks 0 --no-video --no-audio --no-track-tags --no-global-tags --no-chapters "${filename}.srt" \
--track-order "0:${video_track_id},0:${audio_track_id},1:0"
done
Misc.
Extract Fonts from MKV
for i in $*; do
echo $i
ids=`mkvmerge -i "$i" | grep -oP '(?<=Attachment ID )[0-9]+'`
for id in $ids; do
mkvextract attachments "$i" $id
done
done
Extract Subtitles from MKV
for i in ./**/*.mkv; do
echo "$i"
#Subs=`MediaInfo "$i" | grep -A 1 Text | grep ID | head -n 1 | grep -oP "\d+"`
Subs=`mkvmerge -i "$i" | grep 'subtitles' | grep -oP '(?<=Track ID )\d+'`
Name=${i%.*}
mkvextract tracks "$i" ${Subs}:${Name}.ssa
done
Extract Audio from MKV
for i in ./**/*.mkv; do
filename=$(basename "$i" .mkv)
ffmpeg -i "$i" -acodec copy "${filename}.ac3"
done
Recursively Convert / Encode WAV to FLAC
for file in ./**/*.wav; do
echo "Processing $file ...";
flac --delete-input-file --preserve-modtime --verify --best --exhaustive-model-search --keep-foreign-metadata "$file"
done