ffmpeg is commonly used tool to convert and perform various types of manipulation on video clips. This post will discuss about some of the use cases using this tool. Common arguments passed are

  • -r = Set the frame rate
  • -i = Input file
  • -s[:stream_specifier] = Set frame size
  • -pix_fmt = Set pixel format. Use -pix_fmts to show all the supported pixel formats.
  • -f fmt (input/output) = Force input or output file format.
  • -c[:stream_specifier] codec (input/output,per-stream)

Conversion Examples

  • Convert NV21 to NV12
    Input raw file has color space NV21, resolution 1080p
    ffmpeg -pix_fmt nv21 -s 1920x1080 -i 1080p24_nv21.yuv -pix_fmt nv12 1080p24_nv12.yuv
  • Wrap YUV in container
    Input raw file has resolution 1080p. It is encoded using H.264 and wrapped in .mp4 container
    ffmpeg -f rawvideo -pix_fmt nv21 -s:v 1920x1080 -r 30 -i 1080p24_nv21.yuv -c:v libx264 1080p24_nv21.mp4
  • Convert to encoded stream to YUV
    Encoded input stream is converted to raw format (.yuv).
    ffmpeg -i 20230525_output.264 -pix_fmt nv12 out.yuv
  • Swap planes
    ffmpeg -c rawvideo -s 1280x720 -pix_fmt yuv420p -r 25 -i 2.yuv -vf shuffleplanes=0:2:1 2.mp4

    Details about shuffleplanes
    It reorder and/or duplicate video planes. The first plane has the index 0. The default is to keep the input unchanged. It accepts the following parameters:
    • ‘map0’: The index of the input plane to be used as the first output plane.
    • ‘map1’: The index of the input plane to be used as the second output plane.
    • ‘map2’: The index of the input plane to be used as the third output plane.
    • ‘map3’: The index of the input plane to be used as the fourth output plane.
  • Scale video to the required size in pixels. This can result in a distorted video if input and output aspect ratios are different.
    ffmpeg -i input.mov -vf scale=320:240 output.mp4

    To maintain the aspect ration, you can specify just the width, followed by -1 which will calculate the height to maintain the aspect ratio of the input:
    ffmpeg -i input.mov -vf scale=320:-1 output.mp4
  • Convert container from .ts to .mp4
    ffmpeg -i Qtc88.ts Qtc88.mp4
  • Convert container to MP4 and video codec2 to MPEG 2
    ffmpeg -i Qtc88.ts -c:v mpeg2video Qtc88.mp4