VideoHelp Forum
+ Reply to Thread
Results 1 to 1 of 1
Thread
  1. Have you ever needed to take the chapters from an MKV file, and display them in a non-linear editor like Vegas or Premiere? I did, so I came up with a script for this problem.

    Scripts are at the end, only requirements are MKVCleaver and Python3. This will also work on Sony Vegas, or any other NLE that allows you to import an SRT file.

    Step 1 Run your MKV files through MKVCleaver, and export only the Chapters. You'll get XML files doing it this way.

    Step 2 Drag/drop those XML files onto the .bat script below

    Step 3 Import the SRT file into your NLE as a captions file. And that's it!

    As always, remember that file names matter, especially the Python one, and these two scripts have to be in the same folder with your XML.

    Drop XMLs here to convert chapters to SRT.bat

    Code:
    @echo off
    
    FOR %%A IN (%*) DO (
    python3 "MKVCleaver Chapters XML to SRT.py" "%%~nA"
    )
    MKVCleaver Chapters XML to SRT.py
    Code:
    import sys
    import xml.etree.ElementTree as ET
    
    xmlfile = (sys.argv[1]+".xml")
    tree = ET.parse(xmlfile)
    root = tree.getroot()
    
    text =""
    chapters = list()
    loopnum=0
    for elm in root.findall(".//ChapterTimeStart"):
        rawstarttime=str(elm.text[:12])
        starttime= rawstarttime.replace(".",",")
        hrs=int(starttime[0:2])
        mins=int(starttime[3:5])
        secs=int(starttime[6:8])
        ms=int(starttime[9:12])
        
        chap = starttime
        chapters.append(chap)
    
    for i in range (len(chapters)):
        chap = chapters[i]
        start = chapters[i]
        try:
            end = chapters[i+1]
        except IndexError:
            end = start   
        loopnum+=1
        text += f"""{loopnum}
    {start} --> {end}
    Chapter {loopnum}
    
    """
    
    print(text)
    with open(sys.argv[1]+".srt", "a") as myfile:
        myfile.write(text)
    Last edited by seanmcnally; 23rd Jan 2022 at 16:05. Reason: clearer wording
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!