12 votes

FFmpeg - Merging multiple videos containing chapters into one with chapters from originals

Tags: ffmpeg, video

Hello,

I have quite some technical question and my DuckDuckGo-fu seems very weak on this one. I hope it is ok to post questions on Tildes, as it is not really discussion material... but someone can still learn and use whatever come from this.

I have Live Aid concert that I ripped from my DVDs and I wanted to merge the individual video files (there are four) into one long video. I'm on Linux and I'm used to ffmpeg in command line, though I do not know it that much. Each of the input videos has its own chapters and I would like to transfer those chapters into the final video as well. Preferably adding a chapter in between every input video.

I was unable to find if ffmpeg allows for something like that in a single inline command. I may have to export chapters from each input video and add them into one "chapter" file and redo times by hand on them and then use this file as "chapter" input when merging the videos, but all this is just a theory on my part.

Is there some FFmpeg expert here who has done something like that?

13 comments

  1. [3]
    Pavouk106
    (edited )
    Link
    And if somebody wanted to know what I do with FFmpeg - I use it mainly for touch ups on Blu ray rips that I do from my original Blu rays. I use Jellyfin at home (works like Plex) and I build my...

    And if somebody wanted to know what I do with FFmpeg - I use it mainly for touch ups on Blu ray rips that I do from my original Blu rays. I use Jellyfin at home (works like Plex) and I build my media library myself. I rip only English and Czech languages for each movie and then I run it through ffmpeg to lower bitrate a bit and set default language for audio and default subtitles (if any) or set none subtitles as default.

    I did Apollo 13 movie recently. It went with this command:
    ffmpeg -i Videos/Apollo\ 13/Apollo\ 13_t00.mkv -metadata title="Apollo 13" -map 0:0 -c:v:0 libx264 -preset slow -b:v:0 15000k -minrate 15000k -maxrate 15000k -bufsize 15000k -map 0:1 -c:a:0 copy -map 0:2 -c:a:1 copy -map 0:3 -c:a:2 copy -map 0:4 -c:a:3 copy -map 0:5 -map 0:6 -map 0:7 -map 0:8 -map 0:10 -scodec copy -metadata:s:a:2 title="Director's commentary" -metadata:s:a:3 title="Lovell's commentary" -metadata:s:s:2 title="Director's commentary" -metadata:s:s:3 title="Lovell's commentary" -disposition:s:4 default -disposition:a:1 default -disposition:a:0 none Videos/Apollo\ 13.mkv

    This basically re-encodes the video using CPU (because I have some problem with GPU encoding) while copying audio (no re-encoding there) and copying subtitles. It sets the fifth subtitles to be default and sets second audio stream to be default.

    This means the movie will play in Czech language and use subtitles that have only 35 frames through the whole movie (which probably means there will be 1,something second Czech label somewhere through the movie - I still have to see it). Other audio is English, English director's commentary and English Jim Lovell's and his wife's commentary. All audio has it's own subtitles too.

    It's great what can be achieved through command line :)

    Also - if you needed to unset subtitles altogether (so movie plays without any as a default), you would nee to include this in the command:
    -disposition:s -default -default_mode infer_no_subs
    instead of defaulting some of the subtitles.

    I have some other tips up my sleeve too.

    To use NVENC (hardware acceleration on Nvidia cards), you have to substitute libx264 with h264_nvenc. You can also use H265/HEVC with h265_nvenc, though I haven'ŧ used it, so it's just theory on my side. You may run into problems with some other parameters if you go NVENC route because they may be different from using libx264 codec/encoder. FFmpeg will warn you what is wrong.

    If your video is interlaced, you would want to use deinterlacing. For me adding this to the command works:
    -vf yadif=0:-1:0

    EDIT: If somebody wanted to know what the looooong command (or actually all the different parameters) do, just ask. I didn't want to write it all up one by one because of my time and time of all the readers. But if somone who wanted to use that or customize to their liking emerges, I'm willing to help.

    5 votes
    1. [2]
      Akir
      Link Parent
      CPU encoders are generally going to give you higher quality anyways. Hardware encoders generally are built for live transcoding which usually means tradeoffs. I never really bothered with NVENC,...

      CPU encoders are generally going to give you higher quality anyways. Hardware encoders generally are built for live transcoding which usually means tradeoffs. I never really bothered with NVENC, but if it’s anywhere as good as apple’s it shouldn’t really be too bad.

      From my experience bwdif is generally the best deinterlacing filter.

      3 votes
      1. Pavouk106
        Link Parent
        I read that CPU encoding gives better results. But to be honest, I can't see a difference when I actually watch the video (not pausing and trying to pinpoint differences). If I wanted the best...

        I read that CPU encoding gives better results. But to be honest, I can't see a difference when I actually watch the video (not pausing and trying to pinpoint differences). If I wanted the best quality I wouldn't re-encode it anyway. But I really don't need that 30Mbit quality (with that 30Mbit size) to just chill and enjoy the movie. I keep the Blu ray so I can watch origina if I really wanted to (dig up my PS3 and connect it to TV).

        I may try bwdif. Movies are mostly progressive, but extras tend to be interlaced. The best filter for deinterlacing is drumroll to record the video in progressive. I see the technical advantage of interlace historically, but it should be dead today, IMHO.

        4 votes
  2. [4]
    Akir
    Link
    This is doable but it may be a bit of a pain. There is a wiki documentation page about this: https://trac.ffmpeg.org/wiki/Concatenate

    This is doable but it may be a bit of a pain. There is a wiki documentation page about this: https://trac.ffmpeg.org/wiki/Concatenate

    4 votes
    1. [3]
      Pavouk106
      Link Parent
      I know about concatenate, the problem here is how to transfer chapters and add new ones in between the videos. That is the thing I haven't done (yet).

      I know about concatenate, the problem here is how to transfer chapters and add new ones in between the videos. That is the thing I haven't done (yet).

      1 vote
      1. [2]
        Akir
        Link Parent
        I see. Unfortunately chapter metadata is dependent on time, and concatenation means those times are changing. You can export the metadata and manually edit them. See...

        I see. Unfortunately chapter metadata is dependent on time, and concatenation means those times are changing. You can export the metadata and manually edit them. See https://ffmpeg.org/ffmpeg-formats.html#Metadata-1

        But AFAIK there is no automated tool to do this.

        Blu-rays and DVDs have the ability to put more than one title on the disc. Perhaps you can master it to a disk image skip the burning process?

        1. Pavouk106
          Link Parent
          Oh, well... I was afraid there won't be any easy way :-( I'll see when I have some spare time to actually go through this manually. It's one time job (well, two times, actually, as I have also...

          Oh, well... I was afraid there won't be any easy way :-( I'll see when I have some spare time to actually go through this manually. It's one time job (well, two times, actually, as I have also Live 8), so not worth some script to do it for me.

          Thanks for pointing me in the right direction.

          1 vote
  3. [2]
    kovboydan
    Link
    I’ve not tried this specifically and I’m not sure there’s a convenient solution, but I do wonder if mkvtoolnix might be easier to work with than ffmpeg for this.

    I’ve not tried this specifically and I’m not sure there’s a convenient solution, but I do wonder if mkvtoolnix might be easier to work with than ffmpeg for this.

    2 votes
    1. Pavouk106
      Link Parent
      I'll look into that. Thabks for the info.

      I'll look into that. Thabks for the info.

  4. [2]
    Greg
    Link
    Looks like it’s a (fairly old) open issue with the concat demuxer just not supporting chapters: https://trac.ffmpeg.org/ticket/6468. The timecodes shouldn’t inherently be a problem (subtitle files...

    Looks like it’s a (fairly old) open issue with the concat demuxer just not supporting chapters: https://trac.ffmpeg.org/ticket/6468. The timecodes shouldn’t inherently be a problem (subtitle files have them, for example, and those get updated just fine), but someone needs to write the code to do it!

    I did find this short python script (from a blog post) that looks to do exactly that - haven’t had a chance to test it, but may be what you’re looking for?

    1 vote
    1. Pavouk106
      Link Parent
      I'll have a look. If I was a better programmer, I could have written a code similar to sibtitles being transferred. But my spaghetti wouldn't be useful there :-)

      I'll have a look.

      If I was a better programmer, I could have written a code similar to sibtitles being transferred. But my spaghetti wouldn't be useful there :-)

      1 vote
  5. [2]
    kwyjibo
    Link
    I sometimes use ffmpeg for similar purposes, and I found ChatGPT to be an excellent help. Have you tried it? I asked it for you and this was the output: ChatGPT's answer Input: I have two video...

    I sometimes use ffmpeg for similar purposes, and I found ChatGPT to be an excellent help. Have you tried it?

    I asked it for you and this was the output:

    ChatGPT's answer

    Input: I have two video files, both with embedded chapter marks. I want to combine these files and preserve the chapter marks metadata. I want the second video file's chapter marks to adapt to the runtime of the combined video file. I want to use ffmpeg to achieve this. How do I do it?

    Output: Here's how you can do it:

    1. Extract Chapter Information: Before merging the videos, extract the chapter metadata. You can use ffprobe (comes with ffmpeg) to get the chapter information from the video files.

    Here is the ffprobe command to get the chapters:

    ffprobe -loglevel error -select_streams v -show_entries chapter=start_time,end_time,CHAPTERS/tags/title -of csv=p=0 <video-file>
    

    This will output the chapter timestamps and titles as a csv. Repeat this for both files, save the output somewhere, call it chapters1.csv and chapters2.csv.

    1. Adjust Chapter Timestamps: Assuming that you have now two CSV files representing chapters from both videos, you will have to adjust the second CSV's timestamps. You can do this by adding the duration of the first video to the start time and end time of the chapters in the second CSV.

    You can get the duration of a video file like this:

    ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 <video-file>
    

    This will output the duration of the video file in seconds. Add this duration to the start time and end time of all chapters from chapters2.csv.

    It's important to note that the duration should be added only to the second video's chapters considering that those would be appended at the end of the first video.

    1. Concatenate Videos: Concatenate your videos with ffmpeg - there are different ways depending on whether your videos have the same parameters, but assuming they do a simple command would look like:
    ffmpeg -i <input1.mp4> -i <input2.mp4> -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" <output.mp4>
    
    1. Create a Metadata File: The metadata file contains the chapter information that ffmpeg needs. Following is a template you can use:
    ;FFMETADATA1
    [CHAPTER]
    TIMEBASE=1/1000
    START=0
    END=<duration of first chapter in milliseconds>
    title=<title of first chapter>
    [CHAPTER]
    TIMEBASE=1/1000
    START=<start of second chapter in milliseconds>
    END=<end of second chapter in milliseconds>
    title=<title of second chapter>
    ...
    

    Replace the START, END, and title sections with the data from your CSV files. Create a metadata file for all chapters.

    1. Add Chapter Marks: Finally, use ffmpeg to add the chapter marks to the concatenated video.
    ffmpeg -i <concatenated-video> -i <metadata-file> -map_metadata 1 -codec copy <output-video>
    

    Please note: This process involves a lot of manual labor. If you have a programming background, you can consider writing a script to automate most of these tasks. If you do not, you might want to consider using a video editor tool with a graphical user interface specifically built for merging video files and preserving/editing metadata.

    It tells you a lot of manual labor required on your part but all of that can be automated, too. If you're not able to write code, you can probably ask ChatGPT to write you a script for it. Sounds simple enough for it to handle.

    1 vote
    1. Pavouk106
      Link Parent
      This is the olny way I could think of before I asked here. Well... I didn't know the exact way, but thought that I would need to extract chapters and combine them myself and the add then to merged...

      This is the olny way I could think of before I asked here. Well... I didn't know the exact way, but thought that I would need to extract chapters and combine them myself and the add then to merged video.

      I just thought this might be part of ffmpeg that I don't know about. Seems it isn't.

      Thaks for ChatGPT's suggestion. I will probably do it this way if I don't find any other things menioned here useful.

      1 vote