Due to my work, I work with videos often. Not editing them, but making test streams and the utility
ffmpeg has been extremely valuable. I want to start collecting the command lines I use more than once.
Cut Video To Length
ffmpeg -i source.mkv -ss 0:00:00 -to 1:00:00 -c copy target.mkv
-to to trim video.
-c copy will preserve the codec and not re-encode the bitstream, so this operation will be super fast to get a stream approximately the length you want.
-ss option will start your stream from the closest I-picture. Because most likely the frame you specified isn't a keyframe and needs I- and P-pictures to build the complete frame. There is also problem with discontinuous time stamps that some decoders may not like. You can workaround that by making first sample with 0 PTS using
ffmpeg -i source.mkv -vf "crop=width:height:left:top" target.mkv
top to define the rectangular area you want to extract.
You need to specify video and audio codecs you want to use. For example, to make H.264/AAC stream:
ffmpeg -i source.mkv -c:v libx264 -c:a aac target.mp4
Or VP9/Vorbis stream:
ffmpeg -i source.mkv -c:v libvpx-vp9 -c:a libvorbis target.webm
Probably you want more control than just changing the codecs. Look at the links in References section to get examples on the knobs you can tweak. But generally the options you want to look at are:
Change Playback Speed
To double the speed of video for your daughter's school project of baking brownies:
ffmpeg -i source.mkv -vf "setpts=0.5*PTS" target.mkv
To slow down said video so that it is twice as long:
ffmpeg -i source.mkv -vf "setpts=2.0*PTS" target.mkv
Both examples above will re-encode the video. It is possible to manipulate the PTS directly with re-encoding, but there is no guarantee that the decoder you'll be playing back with can keep up.
Test Streams for Hardware Decoder
Some decoders will handle a variety of elementary streams, you just need to feed them with proper PTS information, which is usually only available at layers above ES. Sending ES to libavformat results in AVPacket with
AV_NO_PTS value in this case, not useful for development and testing.
Here I am generating two streams from one single pre-recorded video, that I can read from disk and write to decoders with proper PTS for synchronization. Audio in AAC ADTS:
ffmpeg -i fishtank.mkv -vn -c:a aac -b:a 96k -af "afade=t=in:ss=0:d=13" -avoid_negative_ts make_zero -f mpegts streams/audio.ts
And video in H.264:
ffmpeg -i fishtank.mkv -an -c:v libx264 -r 30 -vf format=yuv420p -profile:v baseline -level:v 4.0 -b:v 1000k -movflags +faststart -avoid_negative_ts make_zero -f vob streams/video.mp4