Sign Up!
Login

Welcome to Monopedilos
Wednesday, September 08 2010 @ 07:43 AM CDT

 

Differences

This shows you the differences between two versions of the page.

mkvmerger [2009/04/30 23:54] (current)
theophile created
Line 1: Line 1:
 +====== mkvmerger ======
 +===== Intro =====
 +The Matroska multimedia container is a versatile next-generation A/V container. Once of its best features is a robust chapter system. Unfortunately, the Matroska tools do not provide an easy way to concatenate multiple Matroska files as individual chapters of a new meta file. This script does that.
 +===== Prerequisites =====
 +  * mplayer
 +  * mkvmerge
 +
 +===== Usage =====
 +The script will prompt the user to enter the name for the new Matroska file. It will then prompt the user to enter a name for each chapter, based on the input file.
 +
 +At the top of the script, there are two options that can be set by the user. The first option is the chapter preview option. Set ENABLE_PREVIEW to 1 and the script will launch mplayer to play each source file in turn to enable to user to see it before entering an appropriate chapter name. Setting this value to anything other than 1 will disable mplayer preview. This is on by default.
 +
 +The second option is SAVE_CHAPTER_FILE. If enabled, this will prompt the user at the beginning to enter a filename for the simple chapter file used by mkvmerge to write the chapter markers. This file will be left behind upon completion. This might be useful if the user wants to inspect it, or use it in the GUI version of mkvmerge for more advanced configuration. This option is off by default.
 +
 +===== The Script =====
 +<code bash>#!/bin/bash
 +
 +ENABLE_PREVIEW= # If you want to preview chapters before being asked to name them, set to 1. Otherwise, leave blank.
 +SAVE_CHAPTER_FILE= # If you want the script to save the generated chapter file, set to 1. Otherwise, leave blank.
 +
 +### Function to convert milliseconds to a timestamp
 +function ms_to_ts # <milliseconds>
 +{
 +        # compute time components from the total milliseconds
 +        h=$(( $1 / (3600 * 1000) ))
 +        m=$(( $1 / (60 * 1000) % 60 ))
 +        int_s=$(( $1 / 1000 % 60 ))
 +        frac_s=$(( $1 % 1000 ))
 +
 +        # print it out formatted, padding with zeros
 +        printf "%02d:%02d:%02d.%02d\n" $h $m $int_s $frac_s
 +}
 +
 +### Build the filename array for mkvmerge
 +declare -a files
 +
 +### Set the base time to zero.
 +StartTime=0
 +
 +### Prompt user to specify the output file (Placeholder until mkvmerge integration)
 +echo -n "Please enter the name for the new metafile you wish to create: "
 +read outfile
 +
 +### Prompt user for chapter file name, or generate a temporary one
 +if [[ $SAVE_CHAPTER_FILE == 1 ]]; then
 + echo -n "Please enter the name for the chapter file that will be generated: "
 + read chapterfile
 +  else
 + chapterfile=`tempfile`       
 +fi
 +
 +### Build the chapter definition file
 +while [[ $# -gt 0 ]]; do
 +
 + ### Set variables
 + filename="$1"
 + chapter=$(( $chapter + 1 )) # Increases the chapter number by 1 each time the loop runs.
 + chapterz=`printf "%02d" $chapter` # Zero pads the chapter number to conform to proper file format
 + DURATION=`mplayer -vo null -ao null -frames 0 -really-quiet -identify "$filename" | grep "ID_LENGTH" | awk -F"=" '{ print $2 }'`
 + ms=$(echo "$DURATION * 1000" | bc | rev | cut -b 4- | rev)
 + FormattedStartTime=$(ms_to_ts $StartTime) # base time converted to milliseconds
 +
 + ### Use mplayer to preview each chapter to help in assigning names
 + if [[ $ENABLE_PREVIEW == 1 ]]; then
 + mplayer -really-quiet "$1"
 + fi
 +
 + ### Create the line with Chapter number and start time:
 + echo "CHAPTER"$chapterz"="$FormattedStartTime"" >> "$chapterfile"
 +
 + ### Prompt user to enter a name for each chapter, then put that on the next line:
 + echo -n "Please enter a title for "$1", which will become Chapter "$chapter": "
 + read c
 + echo "CHAPTER"$chapterz"NAME="$c"" >> "$chapterfile"
 +
 + ### Set new base time
 + StartTime=$(($StartTime + $ms))
 +
 +        # prepend '+' to the filename if it's not the first filename
 +        if [[ ${#files[*]} -gt 0 ]]; then
 +                filename=+$filename
 +        fi
 +        # append the filename to the array
 +        files[${#files[*]}]=$filename
 +
 + shift
 +done
 +
 +mkvmerge -o "$outfile"  "${files[@]}"  --chapters "$chapterfile"
 +
 +if [[ $SAVE_CHAPTER_FILE != 1 ]]; then
 + rm "$chapterfile"
 +fi
 +
 +echo "Done!"</code>
 
Logged in as: Guest (Guest)
mkvmerger.txt · Last modified: 2009/04/30 23:54 by theophile
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2