FFmpeg Use Examples

This topic provides some FFmpeg command use examples.

Inspect File Property

The following code block reads and prints the metadata of an audio file.
ffprobe test1-16k-32b.aac
The following code block reads and prints the metadata of a video file.
ffmpeg -i 1080p_h264_aac.mp4

Decode Video

The following examples decode H.264 and H.265 video streams via FFmpeg OMX plug-ins (h264_omx and hevc_omx).

  • Input with MP4 container:
    ffmpeg  -vcodec h264_omx -i 1080p_h264_aac.mp4  -frames 100 -pix_fmt yuv420p 1080p_h264_aac.yuv
  • Input with AVI container:
    ffmpeg -vcodec h264_omx  -omx_pix_fmt nv21  -i 4K_60Fps_AVC_Highl5_2.avi -frames 70 -pix_fmt nv21 4K_60Fps_AVC_avi_NV21.yuv
  • Output with NV12 format YUV:
    ffmpeg  -vcodec h264_omx -omx_pix_fmt nv12 -i 4K_30FPS_AVC_420_omx.h264 -frames 70 -f rawvideo -pix_fmt nv12 -y ff_4K_AVC_NV12.yuv  -hide_banner -y -v verbose
  • Output with scale to 1280x720 YUV:
    ffmpeg  -vcodec h264_omx -omx_pix_fmt yuv420p -i 720p_30FPS_AVC_420_omx.h264 -frames 70 -f rawvideo -pix_fmt yuv420p -y ff_720p_AVC_i420.yuv -hide_banner -y -v verbose
  • For H265 video decoding:
    ffmpeg  -vcodec hevc_omx -i 1080p_h265_aac.mp4  -frames 100 -pix_fmt yuv420p 1080p_h265_aac.yuv

Encode Video

The following examples encode video streams (in YUV format) into H.265 formats via the FFmpeg OMX plug-in hevc_omx.

  • Output with MP4 container:
    ffmpeg -s 1920*1080 -i 1080p_h264_aac.yuv -vcodec hevc_omx -frames 201080p_h264_aac.mp4
  • Output with AVI container:
    ffmpeg -s 1920*1080 -pix_fmt yuv420p -i 1080P_30Fps_HEVC_High_I420.yuv -vcodec hevc_omx -b:v 5000k -vtag hev1 1080P_30Fps_HEVC.avi
  • Output with MKV container:
    ffmpeg -s 1920*1080 -pix_fmt yuv420p -i 1080P_30Fps_HEVC_High_I420.yuv -vcodec hevc_omx -b:v 5000k 1080P_30Fps_HEVC.mkv
  • Output with MOV container:
    ffmpeg -s 1920*1080 -pix_fmt yuv420p -i 1080P_30Fps_HEVC_High_I420.yuv -vcodec hevc_omx -b:v 5000k 1080P_30Fps_HEVC.mov
  • Encoding support NV12 format:
    ffmpeg  -s 3840*2160 -r 30 -pix_fmt nv12 -i ff_4K_HEVC_NV12.yuv -vcodec hevc_omx -b:v 5000k -y ff_4K_HEVC_NV12_omx_30fps.h265 -hide_banner -y -v verbose
  • Encoding support NV21 format:
    ffmpeg  -s 3840*2160 -r 30 -pix_fmt nv21 -i ff_4K_HEVC_NV21.yuv -vcodec hevc_omx -b:v 5000k -y ff_4K_HEVC_NV21_omx_30fps.h265 -hide_banner -y -v verbose
  • Encoding with bit rate 5 Mbps:
    ffmpeg  -s 1920*1080  -pix_fmt yuv420p -i ff_1080P_HEVC_I420.yuv -vcodec hevc_omx -b:v 5000k -y ff_1080p_HEVC_NV12_omx_25fps.h265  -hide_banner -y -v verbose
  • Encoding with bit rate 5 Mbps and frame rate 15 fps:
    ffmpeg  -s 1920*1080  -r 15 -pix_fmt yuv420p -i ff_1080p_30fps_HEVC_i420.yuv -vcodec hevc_omx -b:v 5000k -y ff_1080p_HEVC_NV12_omx_15fps.h265 -hide_banner -y -v verbose
  • Encoding with GOP30:
    ffmpeg -s 1920*1080 -pix_fmt yuv420p -i 1080p_30fps_HEVC_i420.yuv -vcodec hevc_omx -g 30 ff_1080p_HEVC_i420_omx_g30.h265

Transcode Video

The following code block transcodes and scales an H.264 1920×1080 file to H.265 1280×720.
ffmpeg -v verbose -vcodec h264_omx -scale_width 1280 -scale_height 720 -i /1080p_h264_aac.mp4 -vcodec hevc_omx -acodec copy /out/720p_h265_aac.mp4 -y
The following code block transcodes and scales an H.264 + ACC MP4 file to MPEG4 + MP3 MP4 (through soft transcoding).
ffmpeg -i 1080p_h264_aac.mp4 -vcodec mpeg4 -acodec libmp3lame 1080p_mpeg4_mp3.mp4

Decode JPEG Image

The following examples decode JPEG image files to YUV 420p with no image size scaling via the mjpeg_omx plug-in.

  • Output YUV with NV12 format, scale width to 1/2, and scale height to 1/2:
    ffmpeg -nostdin -nostats -c:v mjpeg_omx -omx_pix_fmt nv12 -scale_width 1 -scale_height 1 -i MJPEG_3840_2160_444.mjpeg ff_MJPEG_1920_1080_nv12_scale.yuv -hide_banner -y -v verbose
  • Output YUV with NV16 format:
    ffmpeg -c:v mjpeg_omx -omx_pix_fmt nv16 -i JPG_3840_2160_444.jpg ff_JPG_3840_2160_nv16.yuv -hide_banner -y -v verbose
  • Output YUV with NV12 format and mirror horizontal:
    ffmpeg -nostdin -nostats -c:v mjpeg_omx -omx_pix_fmt nv12 -mirror 2 -i MJPEG_3840_2160_444.mjpeg ff_MJPEG_3840_2160_nv12_scale.yuv -hide_banner -y -v verbose
  • Output YUV with NV12 format and rotate 90 degrees:
    ffmpeg -nostdin -nostats -c:v mjpeg_omx -omx_pix_fmt nv12 -rotation 1 -i MJPEG_3840_2160_444.mjpeg ff_MJPEG_2160_3840_nv12_scale.yuv -hide_banner -y -v verbose
  • Output YUV with crop at x:800 y:800 width:800 height:800:
    ffmpeg -c:v mjpeg_omx -crop 800,800,800,800 -i JPG_3840_2160_444.jpg ff_JPG_800_800_crop.yuv -hide_banner -y

Decode MPEG Video

The following examples decode MPEG video files into YUV via the mjpeg_omx plug-in.

  • Output YUV and mirror vertical:
    ffmpeg -c:v mjpeg_omx -mirror 1 -i sample_3840x2160.mjpeg ff_MJPEG_3840_2160_I420.yuv -hide_banner -y
  • output YUV and rotation 90 degrees:
    ffmpeg -c:v mjpeg_omx -rotation 1 -i sample_3840x2160.mjpeg ff_MJPEG_3840_2160_rotation.yuv -hide_banner -y
  • Output YUV with crop at x:800 y:800 width:800 height:800:
    ffmpeg -c:v mjpeg_omx -crop 800,800,800,800 -i sample_3840x2160.mjpeg ff_MJPEG_800_800_crop.yuv -hide_banner -y

Capture Video

The following code block captures a 1080p video from an USB camera or sensor camera.
ffmpeg -f v4l2 -t 10 -r 30 -s 1920*1080 -i /dev/video1 test_v4l2.yuv
The following code block captures a 640 × 480 video from an USB camera or sensor camera.
ffmpeg -f v4l2 -t 10 -r 30 -s 640*480 -i /dev/video1 test_v4l2.yuv

Encode and Capture Video

The following examples encode and capture video files and encode them to an H.265 file via the hevc_omx plug-in.

  • Capture from USB camera and encoding:
    ffmpeg -f v4l2 -t 10 -r 30 -s 1920*1080 -i /dev/video4 -vcodec hevc_omx -vb 5000k -pix_fmt yuv420p  ff_uvc_h265_1080p_420_30.h265
  • Capture from IMX219 sensor and encoding:
    ffmpeg -f v4l2 -t 20 -r 30 -s 1920*1080 -i /dev/video1 -vcodec hevc_omx -vb 5000k ff_219_1080p_30.h265

Capture Audio

The following examples capture audio files in different scenarios.

  • Capture audio from wm8960:
    ffmpeg -f alsa -acodec pcm_s32le -i hw:0,1 output-alsa-wm8960.wav
  • Capture audio from wm8960 with 16 K sample rate and 2 channels, 32-bit depth:
    ffmpeg -f alsa -i hw:0,0 -ar 16000 -ac 2 -acodec pcm_s32le ff_wm8960_16k_32b_c2.wav
  • Capture audio from ac108:
    ffmpeg -f alsa -acodec pcm_s32le -i hw:0,1 output-alsa-ac108.wav
  • Capture audio from ac108 with 16 K sample rate and 2 channels, 32-bit depth:
    ffmpeg -f alsa  -ar 16000 -ac 2 -acodec pcm_s32le -i hw:0,0 ff_ac108_16k_32b_c2.wav
  • Capture audio from TDM:
    ffmpeg -f alsa -ar 32000 -ac 2 -acodec pcm_s32le -i hw:0,0 ff_tdm_32k_16b_c2.wav
  • Capture audio from PDM:
    ffmpeg -f alsa -i hw:0,0 -ar 16000 -ac 2 -acodec pcm_s32le ff_pdm_16k_32b_c2.wav

Capture Audio and Video

The following example captures audio and video contents and encoding them into an MP4 file.
ffmpeg -f alsa -acodec pcm_s32le -i hw:0,1 -f v4l2 -s 640*480 -t 20 -r 30 -i /dev/video1 -vcodec h264_omx output-h264_alsa.mp4

Play Audio

  • Play an audio file with PWMDAC:

    Required devices: JH-7110 EVB or VisionFive 2

    ffmpeg -i test1-32k-16b.wav -f alsa -acodec pcm_s16le hw:0,1
  • Play an audio with wm8960:

    Required devices: JH-7110 EVB + wm8960 audio board or VisionFive 2 + wm8960 audio board

    ffmpeg -i test1-16k-32b.wav -f alsa -acodec pcm_s32le hw:0,1

Soft-decode Audio

  • Soft-decode a WAV file to audio RAW PCM:
    ffmpeg -i test1-16k-32b.wav -f s32le -acodec pcm_s32le -ar 16000 -ac 2 output-test1-16k-32b.pcm
  • Soft-decode a MP3 file to audio RAW PCM:
    ffmpeg -i test1-16k-32b.mp3 -f s32le -acodec pcm_s32le -ar 16000 -ac 2 output-test1-16k-32b.pcm
  • Soft-decode a AAC file to audio RAW PCM:
    ffmpeg -i test1-16k-32b.aac -f s32le -acodec pcm_s32le -ar 16000 -ac 2 output-test1-16k-32b.pcm

Soft-encode Audio

  • Soft-encode a RAW PCM file to WAV:
    ffmpeg -f s32le -ac 2 -ar 16000 -i output-test1-16k-32b.pcm -acodec pcm_s32le output-test1-16k-32b.wav
  • Soft-encode a RAW PCM file to AAC:
    ffmpeg -f s32le -ac 2 -ar 16000 -i output-test1-16k-32b.pcm output-test1-16k-32b.aac
  • Soft-encode a RAW PCM file to MP3:
    ffmpeg -f s32le -ac 2 -ar 16000 -i output-test1-16k-32b.pcm output-test1-16k-32b.mp3

Transcode Audio

  • Transcode from WAV to AAC:
    ffmpeg -i test1-16k-32b.wav -acodec aac test1-16k-32b.aac
  • Transcode from WAV to MP3:
    ffmpeg -i test1-16k-32b.wav -acodec libmp3lame test1-16k-32b.mp3
  • Transcode from WAV to OGG Vorbis:
    ffmpeg -i test1-16k-32b.wav -acodec libvorbis test1-16k-32b.ogg
  • Transcode from WAV to FLAC:
    ffmpeg -i test1-16k-32b.wav -acodec flac test1-16k-32b.flac
  • Transcode from sample rate 16 K to 32 K:
    ffmpeg -i test1-16k-32b.mp3 -ar 32000 output-test1-32k-32b.mp3
  • Transcode from 2-channel to 1-channel:
    ffmpeg -i test1-16k-32b.mp3 -ac 1 output-test1-16k-32b-ac1.mp3
  • Transcode bit rate:
    ffmpeg -i test1-16k-32b.mp3 -ab 128k output-test1-16k-32b-ab128k.mp3

Crop Content

  • Crop H.264 content from an H.264 file:
    ffmpeg -i 1080p.h264 -ss 00:00:10 -t 01:00:00 -vcodec copy -acodec copy 1080p.h264
  • Crop content from an audio file beginning at time 00:30:00:
    ffmpeg -i test1-16k-32b.mp3 -ss 00:30:00 -t 00:01:00 -acodec copy output-test1-16k-32b-cut.mp3

Sift-encode and Transcode Image

  • Encode a YUV file to JPG:
    ffmpeg -s 1920*1080 -pix_fmt yuv444p -i output-big_big_image-1920-1080.yuv output-big_big_image-1920-1080.jpg
  • Encode a YUV file to PNG:
    ffmpeg -s 1280*720 -pix_fmt yuv444p -i output-big_big_image-1280-720.yuv out-put-big_big_image-1280-720.png
  • Encode a YUV file to BMP:
    ffmpeg -s 640*480 -pix_fmt yuv444p -i output-big_big_image-640-480.yuv output-big_big_image-640-480.bmp
  • Encode a RGB file to JPG:
    ffmpeg -s 640*960 -pix_fmt rgba -i output_640_960.rgb output_640_960.jpg
  • Encode a RGB file to PNG:
    ffmpeg -s 640*960 -pix_fmt rgba -i output_640_960.rgb output_640_960.png
  • Encode a RGB file to BMP:
    ffmpeg -s 640*960 -pix_fmt rgba -i output_640_960.rgb output_640_960.bmp
  • Transcode a JPG file to PNG:
    ffmpeg -i big_big_image.jpg big_big_image.png
  • Transcode a JPG file to BMP:
    ffmpeg -i big_big_image.jpg big_big_image.bmp
  • Transcode and scale to a specific size:
    ffmpeg -i big_big_image.jpg -vf scale=320:240 big_big_image_320_240.jpg
  • Transcode and scale to a specific ratio:
    ffmpeg -i big_big_image.jpg -vf scale=320:-1 big_big_image_zoom.jpg
  • Decode a video file to image sequence:
    ffmpeg -i 1080p_h264_aac_cut5s.mp4 -t 5 -s 1920*1080 output-cut-img-%3d.jpg
  • Encode an image sequence to video:
    ffmpeg -i output-cut-img-%3d.jpg output-jpg.mp4

Add Subtitle

  • Add subtitles to an MP4 file:
    ffmpeg -i 1080p_h264_aac.mp4 -i test_subtitle.srt -c copy -c:s mov_text out-put-sub-1080p_h264_aac.mp4
  • Add subtitles to an MKV file:
    ffmpeg -i 1080p_h264_aac.mkv -i test_subtitle.srt -c copy output-sub-1080p_h264_aac.mkv

Add Logos

The following example adds a logo to a video file.
ffmpeg -i 1080p_h264_aac_cut5s.mp4 -i logo_320_240.jpg -filter_complex overlay output-1080p_h264_aac_cut5s_logo.mp4

Play Video

  • Play an H.265 video file:
    ffplay -v verbose -vcodec hevc_omx -i /test1_720P.h265
  • Play an MP4 video file:
    ffplay -v verbose -vcodec hevc_omx -f alsa hw:0,1 /720p_h265_aac.mp4
  • Play an audio file:
    ffplay -f s16le -ar 32000 -ac 2test.pcm