1

Often there is a case where one has a series of the already muxed Matroska files, and they have a logical order. It may be desirable to chain them all together, giving a hint to the video player, that after finishing one of these files another one should start playing.

Although the standard permits prev-uid and next-uid metadata tags it can be quite difficult to set them up on all the files, doing it by hand will all but guarantee some human error in pasting, and is way too tidious anyhow.

1 Answer 1

1

The mkvtools package provides a way to read and to edit the metadata of any Matroska file without the need to remux directly from the command line. This allows one to write a relatively simple bash script that does the job.

In here the job is broken into three steps:

  1. UID of each file is read by mkvinfo, cleaned, and recorded into the .uid file. At the same time these files are copied to the previous and next file with the extensions .nextuid and .prevuid.
  2. Option file is generated by taking data from .nextuid and .prevuid files where they exist, this option file is saved with .seqtags extension.
  3. The tool mkvpropedit is used to write the information, and the temp files are deleted.

The reason why it is not done in a single step is to allow the user to abort during the read process and to know that no files have yet been altered.

mkvsequence.sh

#!/bin/bash
# GPL3.0
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

FILES=$(ls *.mkv|sort)

echo First reading all the information
for FILE in $FILES
do
    echo Processing $FILE
    exec mkvinfo "${FILE}" | grep "Segment UID: "  | sed "s/^| *+ Segment UID: //g" > "${FILE}.uid"
    if [ $PREVFILE ]
    then
        cp "${FILE}.uid" "${PREVFILE}.nextuid"
        cp "${PREVFILE}.uid" "${FILE}.prevuid"
    fi
    PREVFILE=$FILE
done

echo Creating the necessary option files
for FILE in $FILES
do
    echo Processing $FILE
    if [ -f "${FILE}.prevuid" -o -f "${FILE}.nextuid" ]
    then
        echo $FILE > "${FILE}.seqtags"
        echo --edit >> "${FILE}.seqtags"
        echo info >> "${FILE}.seqtags"

        if [ -f "${FILE}.prevuid" ]
        then
            echo --set >> "${FILE}.seqtags"
            echo prev-uid=$(cat "${FILE}.prevuid") >> "${FILE}.seqtags"
        fi

        if [ -f "${FILE}.nextuid" ]
        then
            echo --set >> "${FILE}.seqtags"
            echo next-uid=$(cat "${FILE}.nextuid") >> "${FILE}.seqtags"
        fi
    fi
done

echo Now writing all the data
for FILE in $FILES
do
    echo Processing $FILE
    mkvpropedit "@${FILE}.seqtags"
    rm "${FILE}.uid" "${FILE}.prevuid" "${FILE}.nextuid" "${FILE}.seqtags"
done

IFS=$SAVEIFS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.