VideoHelp Forum
+ Reply to Thread
Results 1 to 24 of 24
Thread
  1. I have a number of MP4 files. They have chapters. However, there's no chapter at the start of the file, because whoever created them evidently felt that having a chapter marker there was redundant.

    FFMPEG believes otherwise, because this:
    Code:
    for %F in (*.mp4) do (ffmpeg -i "%F" -acodec copy -vcodec copy -map_chapters 0 "%~nF.mkv")
    results in an MKV file that shifts the first chapter point from wherever it originally was, and puts it at the start. Which basically means that it's deleting that chapter point. All other chapter points seem to be in the correct spot.

    Is there a way to add a chapter point to the start of the mp4 file as part of this batch process, so it all comes out correctly?
    Quote Quote  
  2. So I have to do it manually? For all of them? I have over 200 files here...
    Quote Quote  
  3. Then write a script which exports the chapters adds a chapter at 0 and then add the chapters on remuxing back. (or use a program which already does it, my guess is most guis that can use ffmpeg will do it,..)
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  
  4. If I knew how to write a script that would do that, I wouldn't be on here asking how to do it...
    Quote Quote  
  5. Probably not really something one would want to do with in a Windows batch script, but should be possible.

    The general steps would be:
    a. extract chapters into a readable file
    b. add chapter at zero if non exists
    c. remux
    May be someone with enough knowledge and motivation will show up (most encoding guis like MeGui, StaxRip, etc. which support batch processing and output to mp4 should do this in the background)


    Cu Selur

    Ps.: Did you try using mkvmerge instead of ffmpeg, iirc. mkvmerge also has no problem with this and it's a command line tool.
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  
  6. Wouldn't mkvmerge require having an mkv file to begin with?

    And, again, I have no idea how to go about editing the chapters in an automated way, so simply saying to do that doesn't help me.
    Quote Quote  
  7. Wouldn't mkvmerge require having an mkv file to begin with?
    No, mkvmerge takes lots of different formats as input, but only outputs mkv.
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  
  8. Member
    Join Date
    Feb 2006
    Location
    United States
    Search Comp PM
    Originally Posted by koberulz View Post
    Wouldn't mkvmerge require having an mkv file to begin with?

    And, again, I have no idea how to go about editing the chapters in an automated way, so simply saying to do that doesn't help me.
    try using the ffmetadata format - https://stackoverflow.com/questions/47415660/how-to-add-chapters-into-mp4-mkv-file-using-ffmpeg
    Quote Quote  
  9. Again, that doesn't help with automating the process.

    I tried creating a one-second black video, giving it a chapter right at the start, then using mkvmerge to mux it onto one of the problem mp4 files. It kind of works, the first chapter from the mp4 stays in the correct spot and it adds the chapter from the blank video, but a) they're in separate editions instead of just the one edition and b) when I try to play the output file in VLC it just stays with a black screen and the yellow loading bar trying to load the file.
    Quote Quote  
  10. What do you mean by "they're in separate editions instead of just the one edition" ?
    Got a small sample file which allows to reproduce the problem?
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  
  11. When you view them in the MKVToolnix chapter editor, there are two editions in the file. I don't know what an edition is or how it works, though.
    Quote Quote  
  12. okay.
    -> "Got a small sample file which allows to reproduce the problem?"
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  
  13. Wait, got it. I was adding rather than appending. If I append the one-second, one-chapter black screen video to the existing problem video, I get a perfectly functional and correctly-chaptered MKV file. Woot.
    Quote Quote  
  14. Have you trief just remuxing with mkvtoolnix and not appending anything? (I think mkvmerge should be able to do this without issues and you could use it in a bash script if you want to.)
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  
  15. Well that does work inasmuch as it retains the existing chapter points, but there's still no chapter point at the start of the file, which screws things up.
    Quote Quote  
  16. I thought the issue was that the chapter points were moved,.. "results in an MKV file that shifts the first chapter point from wherever it originally was" -> confused, but it appending worked fine for you okay,...
    users currently on my ignore list: deadrats, Stears555
    Quote Quote  
  17. Not sure if it will help but MKVToolNix has an option called “One chapter for each appended file” in the “Output” tab ; perhaps it would add a chapter point at the beginning of a single input MP4 file, making the rather dirty trick of adding a 1s blank clip unnecessary, although I don't know how it would interact with existing chapters. When you play with MKVTookNix GUI and find an option that works as intended, you can then display the corresponding mkvmerge command line with “Multiplexer” > “Show command line”{*}, and then use that command line directly in a script to batch-process dozens of files in the same way. I tested with a single MP4 file as input, enabling the aforementioned option added this to the command line :
    Code:
     --chapter-language und --generate-chapters-name-template ^"Chapitre ^<NUM:2^>^" --generate-chapters when-appending

    {*} Disclaimer : I'm using a version from 2019 and it has a rather hectic development rate so it could have changed in the mean (really mean) time.
    Quote Quote  
  18. It adds the chapter point at 00:00:00.000, but when you view it in the Toolnix Chapter Editor that's the last chapter on the list. As such, when played back in VLC it shows up as the "last" chapter, which means the entire video is now "Chapter 01" and all the original chapter points are inaccessible.
    Quote Quote  
  19. It's possible as a three-step process though:

    mkvmerge -o "%~nF.mkv" --generate-chapters when-appending "%F.mp4"
    mkvextract "%~nF.mkv" chapters --simple "chapters.txt"
    mkvpropedit "%~nF.mkv" --chapters "chapters.txt"

    Now I just need to figure out what to replace ~nF with so it puts things in the correct subfolder, otherwise I'll still have to run it a couple dozen times.
    Quote Quote  
  20. It adds the chapter point at 00:00:00.000, but when you view it in the Toolnix Chapter Editor that's the last chapter on the list. As such, when played back in VLC it shows up as the "last" chapter, which means the entire video is now "Chapter 01" and all the original chapter points are inaccessible.
    Alright then. At least you found a workaround.

    Now I just need to figure out what to replace ~nF with so it puts things in the correct subfolder, otherwise I'll still have to run it a couple dozen times.
    Not sure what you mean here, but you can specify a folder name before the %~nF parameter / argument, and add a suffix after if needed — in this case to specify a different name for each chapter file, based on the MP4 file base name. Also you can run all three commands in a single loop. For instance :
    Code:
    FOR %%F in (*.mp4) do (
        mkvmerge -o ".\MKV\%%~nF.mkv" --generate-chapters when-appending "%%F.mp4"
        mkvextract ".\MKV\%%~nF.mkv" chapters --simple "%%~nF - chapters.txt"
        mkvpropedit ".\MKV\%%~nF.mkv" --chapters "%%~nF - chapters.txt"
    )
    (This should work as a .bat script, located in the same folder as the MP4 files ; if typing the commands from the command prompt only one “%” percent sign should be used.)
    (The “.” in the example above means “the current folder”, the one where the script file is located and executed from.)
    Quote Quote  
  21. I mean I have the video files in subfolders, so if I want to run it once, I need to save them as %~dpnF.mkv not just %~nF.mkv.
    Last edited by koberulz; 2nd Nov 2021 at 06:49.
    Quote Quote  
  22. Then you could use FOR /R {*}.
    Code:
    FOR /R "E:\name of\the parent folder" %%F in (*.mp4) DO ...

    {*} I like the random quote they put in there :
    “Just think how happy you would be if you lost everything you have right now, and then got it back again.” ~ Frances Rodman
    Quote Quote  
  23. Yes, I know, but my point is if I do that, and save them as %~nF.mkv, they all get saved into whatever folder the command line is running from, rather than the folder the original file was in. They need to be saved as %~dpnF.mkv to end up next to the originals.
    Quote Quote  



Similar Threads

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