4

How to cut away a piece at the start of video from beginning to the next key frame? I know the -ss option

ffmpeg -y -noaccurate_seek -ss 00:00:30 -i "%%~ni.mp4" -avoid_negative_ts make_zero -acodec copy -vcodec copy "%%~ni_cut.mp4"

but using it I should know approximate position of the next keyframe.

Update to make things clear:

I dont't need the beginning of the video. I need the whole video without this small piece at the start.

Update 2:

Ok. I need to comment even more. Please disregard there 30 seconds. I want to throw away the fragment from key frame 0 (position: 0 seconds from beginning) to the key frame 1 from beginning.

1 Answer 1

0

The segment option is designed to cut video by key frames.

The following post shows how to cut a file to multiple segments.
I thought that adding -segment_list_size 1 creates one segment, but it's not working... (the option is only relevant for a list file).

I ended up with an improvised solution using batch-file.
The concept of the suggested solution is:

  • Start FFmpeg as a background process.
  • Terminate the process after the second segment file is created.
  • Delete the extra output files.

I know that the implemented batch-file is not good enough (my knowledge of batch-files in too basic).


Start by creating a sample synthetic video (with audio) file for testing:

ffmpeg -y -f lavfi -i testsrc=size=192x108:rate=1 -f lavfi -i sine=frequency=400 -f lavfi -i sine=frequency=1000 -filter_complex "[1:a][2:a]amix=inputs=2" -vcodec libx264 -g 10 -crf 17 -pix_fmt yuv420p -acodec aac -ar 22050 -t 500 in.mp4
  • rate=1 set the framerate to 1Hz (for testing purpose).
  • -g 10 applies key frame every 10 frames (for testing purpose).

Here is the batch-files:

if exist out01.mp4 del out01.mp4

:: 'Start FFmpeg process at the background
start ffmpeg -y -noaccurate_seek -ss 00:00:30 -i in.mp4 -c copy -f segment -reset_timestamps 1 -segment_list_size 1 out%%02d.mp4

:START

:: 'Break the loop only when the second file (out01.mp4) is created.
if not exist out01.mp4 goto START

:: 'Terminate FFmpeg process (there is going to be an issue when there is more than one FFmpeg process).
taskkill /F /IM ffmpeg.exe

:: 'Delete the redundant output files (may use a for loop or file pattern). Be carful not to delete out00.mp4
del out01.mp4
if exist out02.mp4 del out02.mp4
if exist out03.mp4 del out03.mp4
if exist out04.mp4 del out04.mp4
if exist out05.mp4 del out05.mp4
if exist out06.mp4 del out06.mp4
if exist out07.mp4 del out07.mp4
if exist out08.mp4 del out08.mp4
if exist out09.mp4 del out09.mp4
if exist out10.mp4 del out10.mp4

The output file is out00.mp4 (10 frames from the synthetic video).


Update:

Cutting off the beginning leaving the rest intact - keeping the tail:

You may apply the following stages:

  • Segment the file, and create a list:

     ffmpeg -y -i in.mp4 -c copy -f segment -reset_timestamps 1 -segment_list list.txt -segment_list_type ffconcat out%04d.mp4
    
  • Delete the second line from list.txt (I don't know how to do it using a batch-file).
    Alternately, delete the first file, and use echo command in a loop (use %%a in a batch file and %a in the console):

     del out0000.mp4
     echo ffconcat version 1.0 > list.txt
     for %%a in (out????.mp4) do echo file %%a >> list.txt
    
  • Apply concat demuxer for concatenating the segments:

     ffmpeg -y -f concat -safe 0 -i list.txt -c copy out.mp4
    

In the above example out.mp4 starts from second 10 (frame 0 is a key frame, and frame 10 is the next key frame).

7
  • In need the opposite. I need cut off the beginning leaving the rest intact. I want to have the tail.
    – Paul
    Commented Jan 9, 2022 at 20:45
  • Ok. I need to comment even more. Please disregard there 30 seconds. I want to throw away the fragment from key frame 0 (position: 0 seconds from beginning) to the key frame 1 from beginning.
    – Paul
    Commented Jan 9, 2022 at 21:38
  • I just updated the answer
    – Rotem
    Commented Jan 9, 2022 at 21:39
  • Try: ffmpeg -y -i in.mp4 -ss 00:00:00.01 -c copy out.mp4
    – Rotem
    Commented Jan 9, 2022 at 21:41
  • Unfortunately, such usage results in strange effects: either sound plays not from the beginning or there is a black video fragment at the beginning. They are good again after the first key frame.
    – Paul
    Commented Jan 9, 2022 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .