This is my personal notes about various manipulation on videos using FFmpeg.
1 Speeding up or slowing down
Using setpts video filter (and asetpts for audio or atempo, see wiki) to manipulate the timestamp of each frame:
ffmpeg -i input.mkv -r FPS -filter:v "setpts=FACTOR*PTS" output.mkv
If we speed each frame’s timestamp (PTS) up by ,
Say 4x, then the original frame at 0:04 shall become 0:01 in the output video, following the equation, is the expected timestamp for the frame.
The output frame rate.
For speeding up, the FPS will need a boost in order to prevent frame losses, from FFmpeg wiki:
Note that this method will drop frames to achieve the desired speed. You can avoid dropped frames by specifying a higher output frame rate than the input.
If the original frame rate is and speeding factor is , then it should be
Say you have a 30-second video at 15 frames per second, that is 450 frames. When you speed it up by of 2, the video length is reduced to seconds, if keeping the same frame rate, that only is frames. Therefore, 225 frames are dropped. By boosting up by the same factor of speeding, you can keep all frames.
For audio, combination of video and audio, and more details, see How to speed up/slow down a video.
2 Trimming
Using -t or -to, from ffmpeg(1):
-to and -t are mutually exclusive and -t has priority.
- -t duration (output)
- Stop writing the output after its duration reaches duration. […]
- -to position (output)
- Stop writing the output at position. […]
duration is relative time since the encoding starts, position is absolute to a specified timestamp. The argument of both “may be a number in seconds, or in hh:mm:ss[.xxx] form.”
They can be used in conjunction of -ss:
- -ss position (input/output)
When used as an input option (before -i), seeks in this input file to position. Note the in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. […]
When used as an output option (before an output filename), decodes but discards input until the timestamps reach position.
[…]
It uses the same time format as above.
An example of usage, cut from 0:03, for 125-second duration and at 3:14” mark, using the output option:
ffmpeg -i input.mkv -t 125 -ss 3 output.mkv ffmpeg -i input.mkv -t 00:03:14 -ss 3 output.mkv
Note
If used with speeding, the duration and position are in term of output’s time scale. For instance, a video of 3 minutes to speed up by 2, and you only want 30 seconds from 1:30 to 2:00 in the original, The duration is 15 seconds, not 30; and the position is 1:00 mark not 2:00.
3 Concatenating
3.1 Concat demuxer
FFmpeg wiki has good instruction on concatenation, basically you can use concat demuxer with a list of file, for example:
file 'video1.mkv' file 'video2.mkv'
With this command:
ffmpeg -f concat -i videos.lst -c copy output.mkv
3.2 Concat filter
Using concat filter, for example:
ffmpeg \ -i video1.mkv \ -i video2.mkv \ -filter_complex "[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]" \ -map "[v]" -map "[a]" \ output.mkv
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.