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).