4

I would like to know how to use ffmpeg to remove specific chapters from a video file. Below is an example. I would like to remove all the Chapters marked "Advertisement". I've searched everywhere and I can't seem to find a way. I don't even mind re-encoding, but I just don't want to do it by hand.

 Duration: 00:52:24.45, start: 0.042989, bitrate: 3290 kb/s
    Chapter #0:0: start 0.000000, end 19.142000
    Metadata:
      title           : Video
    Chapter #0:1: start 19.142000, end 34.017000
    Metadata:
      title           : Advertisement
    Chapter #0:2: start 34.017000, end 810.551000
    Metadata:
      title           : Video
    Chapter #0:3: start 810.551000, end 915.283000
    Metadata:
      title           : Advertisement
    Chapter #0:4: start 915.283000, end 1206.940000
    Metadata:
      title           : Video
    Chapter #0:5: start 1206.940000, end 1291.963000
    Metadata:
      title           : Advertisement
    Chapter #0:6: start 1291.963000, end 1614.738000
    Metadata:
      title           : Video
    Chapter #0:7: start 1614.738000, end 1694.709000
    Metadata:
      title           : Advertisement
    Chapter #0:8: start 1694.709000, end 2068.249000
    Metadata:
      title           : Video
    Chapter #0:9: start 2068.249000, end 2173.261000
    Metadata:
      title           : Advertisement
    Chapter #0:10: start 2173.261000, end 2472.440000
    Metadata:
      title           : Video
    Chapter #0:11: start 2472.440000, end 2582.395000
    Metadata:
      title           : Advertisement
    Chapter #0:12: start 2582.395000, end 3143.276000
    Metadata:
      title           : Video
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1280x720, 3088 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 193 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
    Stream #0:2(eng): Data: bin_data (text / 0x74786574)
    Metadata:
      handler_name    : SubtitleHandler
    Stream #0:3: Video: png, rgb24(pc), 640x360 [SAR 2834:2834 DAR 16:9], 90k tbr, 90k tbn, 90k tbc

1 Answer 1

3

This can be done using the concat demuxer. If the Video chapters begin with a keyframe, then encoding can be skipped.

#1 Copy/Paste the following into a text file, or pipe it to the command in the next step

    Chapter #0:0: start 0.000000, end 19.142000
    Metadata:
      title           : Video
    Chapter #0:1: start 19.142000, end 34.017000
    Metadata:
      title           : Advertisement
    Chapter #0:2: start 34.017000, end 810.551000
    Metadata:
      title           : Video
    Chapter #0:3: start 810.551000, end 915.283000
    Metadata:
      title           : Advertisement
    Chapter #0:4: start 915.283000, end 1206.940000
    Metadata:
      title           : Video
    Chapter #0:5: start 1206.940000, end 1291.963000
    Metadata:
      title           : Advertisement
    Chapter #0:6: start 1291.963000, end 1614.738000
    Metadata:
      title           : Video
    Chapter #0:7: start 1614.738000, end 1694.709000
    Metadata:
      title           : Advertisement
    Chapter #0:8: start 1694.709000, end 2068.249000
    Metadata:
      title           : Video
    Chapter #0:9: start 2068.249000, end 2173.261000
    Metadata:
      title           : Advertisement
    Chapter #0:10: start 2173.261000, end 2472.440000
    Metadata:
      title           : Video
    Chapter #0:11: start 2472.440000, end 2582.395000
    Metadata:
      title           : Advertisement
    Chapter #0:12: start 2582.395000, end 3143.276000
    Metadata:
      title           : Video

#2 Prepare the text file for concat

It should end up like this

file 'myfile.mp4'
inpoint 0.000000
outpoint 19.142000
file 'myfile.mp4'
inpoint 34.017000
outpoint 810.551000
file 'myfile.mp4'
inpoint 915.283000
outpoint 1206.940000
file 'myfile.mp4'
inpoint 1291.963000
outpoint 1614.738000
file 'myfile.mp4'
inpoint 1694.709000
outpoint 2068.249000
file 'myfile.mp4'
inpoint 2173.261000
outpoint 2472.440000
file 'myfile.mp4'
inpoint 2582.395000
outpoint 3143.276000

Each set of three lines corresponds to an included video chapter. The file entry is on the first line, chapter start and end times on the 2nd and 3rd lines.

I used the following command to generate it

grep -B2 'Video' chp.txt | awk '(NR-1) % 4 == 0' | sed -E 's/\ *Chapter #[0-9]:[0-9]+:/file \'myfile.mp4\'\n/g;s/\ *start/inpoint/g;s/,\ end/\noutpoint/g' > list.txt

If you don't have these utilities (if you're on Windows), get them from https://github.com/bmatzelle/gow/releases

#3 Concat the files

ffmpeg -f concat -i list.txt -c copy stripped.mp4

If the Video chapters don't start with a keyframe, you'll have to re-encode.

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.