title: Basic FFMpeg tags: ffmpeg ```bash # convert with default settings ffmpeg -i input.mkv output.mp4 # convert to mp4 using nvidia nvenc # where -b:v is suitably set, e.g 2M for 720p ffmpeg -i input.mkv -c:v h264_nvenc -b:v 4M output.mp4 # as above but don't re-encode audio ffmpeg -i input.mkv -c:v h264_nvenc -b:v 4M -c:a copy out.mp4 # extract audio as wav ffmpeg -i input.mp4 output.wav # extract aac audio from mp4 ffmpeg -i input.mp4 -vn -c:a copy output.m4a # don't reencode anything (just change metadata or container format) ffmpeg -i input.mp4 -c copy output.webm # don't reencode audio, but scale video ffmpeg -i input.mp4 -c:a copy -vf scale=w=1280:h=720 output.mp4 ``` ## Metadata ```bash # extract metadata ffmpeg -i input.mp4 -f ffmetadata metadata.txt # writes metadata to metadata.txt # applying metadata ffmpeg -i input.mp4 -f ffmetadata -i metadata.txt -map_metadata 1 ...other_opts... output.mp4 # specifying metadata ffmpeg -i input.mp4 -metadata artist="Mr Flibble" -metadata title="Hex Vision" -c copy output.mp4 ``` ## Loop Static Image From [stackoverflow](https://stackoverflow.com/questions/25891342/creating-a-video-from-a-single-image-for-a-specific-duration-in-ffmpeg) ```bash ffmpeg -loop 1 -i image.png -c:v libx264 -t 15 -pix_fmt yuv420p -vf scale=320:240 out.mp4 ``` * The -t 15 makes it 15 seconds long. * The -vf scale=320:240 sets the width/height. See MakeStaticMusicVideo. ## Audio Filters A 200Hz highpass filter ``` ffmpeg -i in.wav -af highpass=f=200 out.wav # 12dB/oct ffmpeg -i in.wav -af highpass=f=200,highpass=f=200 out.wav # 24dB/oct ``` Here is a [batch converter to apply a highpass filter](BatchHpFilter) ## Audio Options ``` -an # no audio -ac 2 # 2 audio channels -ac 1 # 1 audio channel -b:a 192k # bitrate -b:a:0 384k -b:a:1 192k # set bitrates for each audio stream ``` ### Set Default Audio Track From [this askubuntu](https://askubuntu.com/questions/1329432/how-to-change-default-audio-track-using-ffmpeg) ``` ffmpeg -i input.mkv -map 0:v:0 \ -map 0:a:2 -map 0:a:0 -map 0:a:1 -map 0:a:3 \ -map 0:s -c copy \ -disposition:a:0 default \ reordered.mkv ``` ### Sample Rate ``` -ar 44100 # audio sample rate ``` You can also use sox: ``` sox in.wav -r 44100 out.wav # will output signed 16bit by default ffmpeg -i a.wav -ar 96000 -c:a pcm_f32le c.wav # 32bit float (little endian) ffmpeg -i a.wav -ar 96000 -c:a pcm_s24le d.wav # 24bit int (signed little endian) ``` ### Raw Audio See [ffmpeg wiki](https://trac.ffmpeg.org/wiki/audio%20types#:~:text=FFmpeg%20can%20read%20various%20raw,Or%20convert%20between%20raw%20types.) ``` # raw input ffmpeg -f s32le input_filename.raw output.wav ffmpeg -f u16le -ar 44100 -ac 1 -i input.raw output.wav ffmpeg -i input -c:a pcm_s32le output.wav # raw output ffmpeg -i input -f s16le -c:a pcm_s16le output.raw ``` ## Get Metadata as Json ```bash ffprobe -v quiet -show_format -show_streams -print_format json input.mp4 ffprobe -v quiet -show_format -print_format json input.mp4 ffprobe -v quiet -show_streams -print_format json input.mp4 ``` ## Deinterlace ```bash ffmpeg -i in.mp4 -vf yadif -o out.mp4 ``` ## Changing Framerate See [[RetimeVideo]]. Note that e.g. playing 24fps video in a 23.98 project will drop one frame per 1000 frames, which is roughly \(1000/23.98\simeq 41.7\) seconds. Likewise playing 30fps at 29.97fps will drop one frame per 1000, and in the case of 30fps, this is \(1000/29.97\simeq 33.4\) seconds. The main use for retime is when you want to put something 25fps into a 24fps project, when timestretching the audio and having a slight slow motion effect is preferable to dropping a frame per second. ## Crop and Expand To crop 16:9 to 4:3, for example (for 1080p): ```bash ffmpeg -i in.mp4 -vf crop=x=240:y=0:w=1440:h=1080 out.mp4 ``` To do the opposite, going from 4:3 to 16:9 (again 1080p in this example): (source [this stackoverflow](https://stackoverflow.com/questions/48485799/ffmpeg-extend-not-resize-video-size-by-adding-box-or-border)) ```bash ffmpeg -i input.mp4 -vcodec libx264 \ -vf "pad=width=1280:height=720:x=0:y=1:color=black" -acodec copy result.mkv ```