Skip to content

Instantly share code, notes, and snippets.

@GaProgMan
Last active August 24, 2016 12:31
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 GaProgMan/c8e66b437734077ad5edc604964f7c98 to your computer and use it in GitHub Desktop.
Save GaProgMan/c8e66b437734077ad5edc604964f7c98 to your computer and use it in GitHub Desktop.
Calls avconv on all mkv container files found in the folder on which it is run.
# assumes that all subtitles have been removed
# and only one audio stream (and it is located at 0:1)
# TODO: add support for all subtitle and audio tracks
for file in *.mkv
do
# extract and convert audio
echo Extracting audio from "$file"
avconv -i "$file" -map 0:1 -c:a aac -b:a 160k -strict experimental audio_$(basename "$file" .mkv).m4a
# 1st pass video
echo Performing first pass of video for "$file"
avconv -i "$file" -c:v libx264 -preset medium -b:v 1024k -pass 1 -s 720x480 -f null -
# 2nd pass video
echo Performing second pass of video for "$file"
avconv -i "$file" -map 0:0 -c:v libx264 -preset medium -b:v 1024k -pass 2 -s 720x480 video_$(basename "$file" .mkv).mov
# combine video and audio
echo Muxing video and audio for "$file"
avconv -i video_$(basename "$file" .mkv).mov -i audio_$(basename "$file" .mkv).m4a -c copy remuxed_"$file"
# cleaning up folder
echo Cleaning up all temporary files for "$file"
find . -name "*.m4a" -type f -delete
find . -name "*.mov" -type f -delete
find . -name "*.log" -type f -delete
find . -name "*.mbtree" -type f -delete
done
for file in *.mov
do
# Rename all .mov files to mkv ones.
# Do this outsite of the above loop, so that the above loop
# isn't called recursively forever.
mv "$file" basename("$file", .mov).mkv
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment