A collection of FFmpeg commands I find helpful when working with and sharing videos.
Convert to MP4
ffmpeg -i input.avi output.mp4
Convert from MKV to MP4 Without Re-encoding
This obviously only works the MKV contains a video that can be put in an MP4, like h264 or h265.
ffmpeg -i input.mkv -c copy output.mp4
Or you can specify the audio and video separately (and convert the audio by leaving it out, for example).
ffmpeg -i input.mkv -vcodec copy -acodec copy output.mp4 ffmpeg -i input.mkv -vcodec copy output.mp4
ffmpeg -i input60fps.mp4 -r 30 output30fps.mp4
Compress For Sharing On The Web
ffmpeg -i input.mp4 -c:v libx264 -crf 25 -c:a aac -movflags faststart output.mp4
The compression factor is set with
-crf, the ffmpeg default for h264 is 23 and 28 for h265.
With x264 and x265, you can set the values between 0 and 51, where lower values would result in better quality, at the expense of higher file sizes. Higher values mean more compression, but at some point you will notice the quality degradation.
Extract Part of Video
-ss to seek to a part of the video.
-t for the duration, or
-to for the time in the video
The behaviour depends on the position of the
-ss parameter. These commands will extract the 10s from 0:30-0:40 in the original video.
ffmpeg -i in.mp4 -ss 30 -t 10 -c copy out.mp4 ffmpeg -i in.mp4 -ss 30 -to 40 -c copy out.mp4
However, if the seek is placed before the file input then timestamps are relative to the seek and these commands now behave differently from each other. With the first output being the same as the original commands, but the second now extracting the 40s long segment from 0:30 to 1:10 in the video.
ffmpeg -ss 30 -i in.mp4 -t 10 -c copy out.mp4 ffmpeg -ss 30 -i in.mp4 -to 40 -c copy out.mp4
Extract Audio From Video
To extract audio from a video you just have to specify an audio file output and it'll ignore the video.
ffmpeg -i input.mkv output.mp3
To extract the raw audio from an MP4 without re-encoding (this depends on what codec the audio data actually is inside the mp4, for most H264 video mp4 files this is AAC audio).
ffmpeg -i input.mp4 -c copy output.aac
Extract Frame Image
Good for quickly getting a thumbnail of the video.
ffmpeg -ss 00:23:00 -i input.mp4 -frames:v 1 output.jpg ffmpeg -i input.mp4 -ss 00:00:10.5 -vframes 1 output.png