Using Kdenlive as matroska(MKV) chapter editor

Step-by-Step descriptions of how to do things.
Post Reply
User avatar
^rooker
Site Admin
Posts: 1481
Joined: Fri Aug 29, 2003 8:39 pm

Using Kdenlive as matroska(MKV) chapter editor

Post by ^rooker »

I've desperately been looking for a simple tool that lets me navigate within a video and set markers, so I can use them as MKV-chapters.

The mkvtoolnix application "mkvmerge" has a nice chapter editor - *if* you already have all the chapters in a suitable textfile format.

I've decided to use kdenlive as visual editor for this job and use its "marker" feature.

The task is as follows:
a) open the video file in kdenlive
b) navigate through the video and add markers as needed
c) save the kdenlive project file
d) use a XSLT file to transform the .kdenlive XML file to a mkvmerge compatible chapters XML.

Here is the XSLT I've written to extract the kdenlive markers:

Code: Select all

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <Chapters>
  <EditionEntry>
  
      <xsl:for-each select="mlt/kdenlivedoc/markers/marker">
        <xsl:variable name="seconds" select="@time mod 60" />
        <xsl:variable name="minutes" select="floor(@time div 60) mod 60" />
        <xsl:variable name="hours" select="floor((@time div 60) div 60)" />
        <!-- hh:mm:ss.msec -->
        <xsl:variable name="timecode">
          <xsl:value-of select="format-number($hours, '00')"/>:<xsl:value-of select="format-number($minutes, '00')"/>:<xsl:value-of select="format-number($seconds, '00.000')"/>
        </xsl:variable>
            
        <ChapterAtom>
          <ChapterDisplay>
            <ChapterString>
              <xsl:value-of select="@comment"/>
            </ChapterString>
          </ChapterDisplay>
          <ChapterFlagHidden>0</ChapterFlagHidden>
          <ChapterFlagEnabled>1</ChapterFlagEnabled>
          <ChapterTimeStart>
            <xsl:value-of select="$timecode"/>
          </ChapterTimeStart>
        </ChapterAtom>
      </xsl:for-each>
      
    
  </EditionEntry>
  </Chapters>
</xsl:template>

</xsl:stylesheet> 
Jumping out of an airplane is not a basic instinct. Neither is breathing underwater. But put the two together and you're traveling through space!
f_brennan
Absolute Beginner
Posts: 1
Joined: Wed Dec 12, 2018 4:23 pm

Re: Using Kdenlive as matroska(MKV) chapter editor

Post by f_brennan »

Thank you so much for your XSLT! Unfortunately it's out of date...I'm not sure as of when, but it definitely doesn't work anymore with kdenlive 18.08.3.

However, I updated it :) Unfortunately the schema has gotten worse than before, not better, so my new one is a bit hacky. However it does work.

Code: Select all

<?xml version="1.0"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <Chapters>
  <EditionEntry>

      <xsl:for-each select="mlt/playlist/property[contains(@name, 'marker')]">
        <xsl:variable name="step1" select="translate(@name, 'kdenlive:marker', '')"/>
        <xsl:variable name="time" select="substring($step1, 3, string-length($step1) - 3)"/>

        <xsl:variable name="seconds" select="$time mod 60" />
        <xsl:variable name="minutes" select="floor($time div 60) mod 60" />
        <xsl:variable name="hours" select="floor(($time div 60) div 60)" />
        <!-- hh:mm:ss.msec -->
        <xsl:variable name="timecode">
          <xsl:value-of select="format-number($hours, '00')"/>:<xsl:value-of select="format-number($minutes, '00')"/>:<xsl:value-of select="format-number($seconds, '00.000')"/>
        </xsl:variable>

        <ChapterAtom>
          <ChapterDisplay>
            <ChapterString>
              <xsl:value-of select="text()"/>
            </ChapterString>
          </ChapterDisplay>
          <ChapterFlagHidden>0</ChapterFlagHidden>
          <ChapterFlagEnabled>1</ChapterFlagEnabled>
          <ChapterTimeStart>
            <xsl:value-of select="$timecode"/>
          </ChapterTimeStart>
        </ChapterAtom>
      </xsl:for-each>


  </EditionEntry>
  </Chapters>
</xsl:template>
</xsl:stylesheet>
You can then make the chapters e.g. thus:

Code: Select all

xsltproc 4subs.xslt 4subs.kdenlive > chaps
And then merge chapters to file thus:

Code: Select all

mkvmerge --chapters chaps -o cm2.mkv cm.mkv
User avatar
^rooker
Site Admin
Posts: 1481
Joined: Fri Aug 29, 2003 8:39 pm

Re: Using Kdenlive as matroska(MKV) chapter editor

Post by ^rooker »

I've frozen my kdenlive version to a stable build a long time ago, so I didn't notice... :shock: :oops:

Thanks for the update! :D
Jumping out of an airplane is not a basic instinct. Neither is breathing underwater. But put the two together and you're traveling through space!
Post Reply