0

Using following code, I'm able to trim video by time:

ffmpeg -i input.mp4 -filter_complex \
"[0:v]trim=60:65,setpts=PTS-STARTPTS[v0]; \
 [0:a]atrim=60:65,asetpts=PTS-STARTPTS[a0]; \
 [0:v]trim=120:125,setpts=PTS-STARTPTS[v1];
 [0:a]atrim=120:125,asetpts=PTS-STARTPTS[a1]; \
 [v0][a0][v1][a1]concat=n=2:v=1:a=1[out]" \
-map "[out]" output.mp4

Above code trim video from 60-65 sec & 120-125sec then concatenate in output.mp4 file.

Need to know how to trim video by using frame number and concatenate.


Is it possible to get the time by using Frame Number and fps?

frame_1_start = 100 #Frame Number
frame_1_end   = 200 #Frame Number
frame_2_start = 450 #Frame Number
frame_3_end   = 700 #Frame Number
fps = 20 # Frame per second

time_x_1 = frame_1_start/fps
time_x_2 = frame_1_end/fps
time_y_1 = frame_2_start/fps
time_y_2 = frame_2_end/fps

ffmpeg -i input.mp4 -filter_complex \
"[0:v]trim=#{time_x_1}:#{time_x_2},setpts=PTS-STARTPTS[v0]; \
 [0:a]atrim=#{time_x_1}:#{time_x_2},asetpts=PTS-STARTPTS[a0]; \
 [0:v]trim=#{time_y_1}:#{time_y_2},setpts=PTS-STARTPTS[v1];
 [0:a]atrim=#{time_y_1}:#{time_y_2},asetpts=PTS-STARTPTS[a1]; \
 [v0][a0][v1][a1]concat=n=2:v=1:a=1[out]" \
-map "[out]" output.mp4
puneet18
  • 4,341
  • 2
  • 21
  • 27

1 Answers1

2

As shown in the trim filter documentation use the start_frame and end_frame options.

Video only

ffmpeg -i input.mp4 -filter_complex \
"[0:v]trim=start_frame=25:end_frame=100,setpts=PTS-STARTPTS[v0]; \
 [0:v]trim=start_frame=200:end_frame=300,setpts=PTS-STARTPTS[v1];
 [v0][v1]concat=n=2:v=1:a=0[v]" \
-map "[v]" output.mp4

Video & audio

ffmpeg -i input.mp4 -filter_complex \
"[0:v]trim=start_frame=25:end_frame=100,setpts=PTS-STARTPTS[v0]; \
 [0:a]atrim=1:4,asetpts=PTS-STARTPTS[a0]; \
 [0:v]trim=start_frame=200:end_frame=300,setpts=PTS-STARTPTS[v1];
 [0:a]atrim=8:12,asetpts=PTS-STARTPTS[a1]; \
 [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" output.mp4

For atrim you can use timestamps as in your original command, or use start_sample and end_sample if you prefer to use audio samples instead. atrim does not have start_frame and end_frame.

For example, if frame rate is 25, and you want trim to include frames 25-100, then atrim would use atrim=1:4.

You can use ffprobe to get frame rate and to check if an input has audio.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • I've only frame numbers, no timestamps. My requirement is that input is array of frame numbers and I've to cut video according to frame numbers and concatenate it. – puneet18 Aug 30 '20 at 18:58
  • Is it possible to get the time by using Frame Number and fps? Please check code in question – puneet18 Aug 30 '20 at 19:05
  • @puneet18 You'll have to convert frame number to timestamp, or frame to audio sample. You will have to do this manually. No such option in atrim to convert video frame to audio duration/sample. – llogan Aug 30 '20 at 19:14
  • can you please suggest how to convert frame number to timestamp, or frame to audio sample? Also check code in question for conversion of frame number to timestamp, is it correct? – puneet18 Aug 30 '20 at 19:17
  • Thanks for your answer. Just curious to know is my answer correct for conversion of frame number to timestamp ? ie timestamp=FrameNumber/fps – puneet18 Aug 30 '20 at 19:21
  • @puneet18 Yes, timestamp=FrameNumber/fps – llogan Aug 30 '20 at 19:22
  • if I run your code on video files which has no audio then it throws error. Stream specifier ':a' in filtergraph description [0:v]trim=start_frame=25:end_frame=100,setpts=PTS-STARTPTS[v0]; [0:a]atrim=1:4,asetpts=PTS-STARTPTS[a0]; [0:v]trim=start_frame=200:end_frame=300,setpts=PTS-STARTPTS[v1]; [0:a]atrim=8:12,asetpts=PTS-STARTPTS[a1]; [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a] matches no streams. – puneet18 Aug 31 '20 at 07:26
  • How can I determine video has audio or not? – puneet18 Aug 31 '20 at 07:26
  • 1
    @puneet18 It will fail if there is no audio. See updated answer for video only example and for link to ffprobe command to check for audio. – llogan Aug 31 '20 at 18:26
  • please check https://stackoverflow.com/questions/63714902/why-ffmpeg-process-is-slow – puneet18 Sep 02 '20 at 23:48
  • I am getting Invalid pixel aspect ratio 3875/3864, limit is 255/255 reducing error in ffmpeg 4.3, any one can help ? – akshay shringi May 17 '21 at 22:00
  • @akshayshringi I recommend asking a new question at [su]. Make sure to show your full command and the **complete** log and I'll take a look. – llogan May 17 '21 at 22:02
  • ffmpeg -i input.mp4 -filter_complex \ "[0:v]trim=start_frame=25:end_frame=100,setpts=PTS-STARTPTS[v0]; \ [0:a]atrim=1:4,asetpts=PTS-STARTPTS[a0]; \ [0:v]trim=start_frame=200:end_frame=300,setpts=PTS-STARTPTS[v1]; [0:a]atrim=8:12,asetpts=PTS-STARTPTS[a1]; \ [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]" \ -map "[v]" -map "[a]" output.mp4 geting [mpeg4 @ 0x2b14e80] Invalid pixel aspect ratio 3875/3864, limit is 255/255 reducing i am trying to trim multiple parts of video and audio, this is not working with only ffmpeg4.3 and cant change ffmpeg – akshay shringi May 17 '21 at 22:08
  • please check this question @llogan https://stackoverflow.com/questions/67577717/ffmpeg-4-3-multipart-trim-issue – akshay shringi May 17 '21 at 22:22
  • @llogan please check if you can help , please – akshay shringi May 17 '21 at 22:37