#] #] ********************* #] "$d_SysMaint"'video/ffmpeg notes.txt' www.BillHowell.ca 24Jul2021 initial To view this file - use a text editor (not word processor) constant width font (eg courrier 10), tab - 3 spaces 24************************24 # Table of Contents : # $ grep "^#]" "$d_SysMaint"'video/ffmpeg notes.txt' | sed 's/^#\]/ /' ********************* "$d_SysMaint"'video/ffmpeg notes.txt' 21Jul2021 to cut existing video ito segments, just use : "$d_bin""video production/trim video.sh" 24************************24 08********08 #] ??Oct2022 08********08 #] 25Oct2022 h264 mp4 codec doesn't work use ffmpeg to convert to ogv!?!? $ finn="$d_PROJECTS"'Family/Steve & Diane/221025 Steve Howell - dunno no description.mp4' $ fout="$d_PROJECTS"'Family/Steve & Diane/221025 Steve Howell - dunno no description.ogv' $ ffmpeg -i "$finn" "$fout" >> HAh hah! Stream mapping: Stream #0:0 -> #0:0 (? (?) -> theora (libtheora)) This build of ffmpeg does not include a "h264" decoder needed for input stream #0:0. 08********08 #] 21Jul2021 to cut existing video ito segments, just use : "$d_bin""video production/trim video.sh" 08********08 #] 24Jul2021 search "ffmpeg and split files?" +-----+ https://medium.com/@taylorjdawson/splitting-a-video-with-ffmpeg-the-great-mystical-magical-video-tool-%EF%B8%8F-1b31385221bd The Split — ffmpeg -i input.mp4 -c:v libx264 -crf 22 -map 0 -segment_time 1 -reset_timestamps 1 -g 30 -sc_threshold 0 -force_key_frames "expr:gte(t,n_forced*1)" -f segment output%03d.mp4 Break it down for me… ffmpeg -i input.mp4 — Invoke the tool and pass it an input file via -i . Source. -c:v libx264 — or -codec:v or -vcodec specifies the codec. Source. -crf 22 —Constant Rate Factor: the default quality (and rate control) setting. Valid range is 0-51. A lower value is a higher quality. For x264, sane values are between 18 and 28. The default is 23. A lot more info here and here. -map 0 — map ALL streams (video, audio, etc.) from the first input file to output. Source. -segment_time 1— The length in n seconds of the video segment. In this case, 1 second. Source. reset_timestamps 1 —Reset timestamps at the beginning of each segment. (This fixed my black video issue). Default 0. Stackexchange. Docs. -g 30 — Group of Pictures. Essentially the length of the segments in frames. For a 2 second clip with an fps of 30 one would set a GOP of 60: framerate * n seconds. Source. Stackexchange. -sc_threshold 0 — Adjusts the sensitivity of x264’s scenecut detection. Setting to 0 disables scene detection. This is necessary for even splits. Source. Supplemental. -force_key_frames "expr:gte(t,n_forced*1)" — Ahh!? An ffmpeg expression to force key frames every n seconds, in this case every 1 second. Thank Paul for the more detailed explanation. -f segment — Format as segment (alias for stream_segment). Source. output%03d.mp4 — Output file padded with 3 0s. I really wanted to have a one stop cohesive answer to this question that I’ve seen half answered several different places. Please comment if it was helpful, if you had issues, or find an error in this post. Hope that helps! Additional Resources: Slicing Video File into Several Segments +-----+ https://stackoverflow.com/questions/64834636/how-to-split-ffmpeg-output-into-multiple-files Using ffmpeg to split files is unusual but it can be done. Basic template is ffmpeg -f data -start START -end END -i subfile:/path/to/file -map 0 -c copy -f data segX.mp4 START and END are byte offsets. The byte at offset START will be the first byte in the output. The byte at offset END will be the first byte not in the output, so output ends with END-1. For next command, start offset is END and end offset is END+X where X is segment size. Check for file size of output. When it's zero, you know the entire file has been split. Share Follow answered Nov 14 '20 at 18:38 Gyan +--+ Alright I've solved my problem, although my question was not worded as precisely as I should've. The root problem I was trying to solve is fragmenting the conversion of some random file type (say flv) to multiple mp4 files on the fly, such that the browser can call sourceBuffer.appendBuffer() on each file. The correct answer, as it turns out, was indeed to use segment. The reason why the segment command in my question was incorrect was that it didn't pass the correct movflags for segmenting. I had failed with this command: ffmpeg -i ../flv.flv -movflags frag_keyframe+empty_moov+default_base_moof -segment_time 5 -f segment -t 20 %d.mp4 The correct command is this: ffmpeg -i ../flv.flv -g 1 -segment_format_options movflags=+frag_keyframe+empty_moov+default_base_moof -segment_time 5 -f segment -t 20 %d.mp4 What is the key difference? The -segment_format_options flag. I guess the failed command totally ignored the -movflags option. Must pass in the movflags to -segment_format_options One additional note: The -g 1 is actually important if you want to appendBuffer as sourceBuffer.mode = 'segment' instead of as 'sequence'. The reason you'd want 'segment' instead of 'sequence' is so that you can append buffers out of order (say a buffer at 30 minute mark straight away). This is useful for a feature such as seeking. My takeaway from this struggle is that FFmpeg is one heck of a beast. Just knowing the ins and outs of FFmpeg would be very impressive and possibly very valuable. I should write a blog post about this one day! I'll post a link to the write up later. Share Follow answered Nov 15 '20 at 6:52 Friendly Genius 17511 silver badge99 bronze badges 1 1) With segment, all files are valid MP4s and independent, Your Q indicates you only wanted the first file to be valid; it also omits mention of movflags in your segmenting attempt. 2) g 1 is very inefficient - files will be much larger for same quality. For quick enough seek, KF at half-second intervals is usually acceptable. – Gyan Nov 15 '20 at 7:42 re 1) ya, I admit my question should've stated my original problem better re 2) what is a better alternative to -g 1 then? – Friendly Genius Nov 15 '20 at 8:41 Use -force_key_frames expr:gte(t,n_forced*0.5) for keyframes every 0.5 seconds. You may have to escape some of those characters depending on shell. – Gyan Nov 15 '20 at 9:13 I just tested and that worked like a charm! Thank you and I'm sorry for the rabbit hole we both went down earlier. Are you looking for some Stackoverflow points? You can write a different answer including your force_key_frames and I'll accept it. Make it sound good. – Friendly Genius Nov 15 '20 at 11:37 force_key_frames has already been documented elsewhere, like stackoverflow.com/q/43563208 . My answer, as written, addresses your question, as currently stated, and this use-case is fairly unique, so more useful than making it another answer about GOP size. – Gyan Nov 15 '20 at 12:40 # enddoc