Convert Video from Different Formats

By Xah Lee. Date: . Last updated: .

You need to have ffmpeg installed. It's usually installed on linux.

If you don't have ffmpeg, try avconv. For some years around 2015, avconv is rename of ffmpeg.

Convert mov to MP4

ffmpeg -f mov -i x.mov x.mp4

Note, .mov is Apple's movie format.

Convert webm to MP4

ffmpeg -f webm -i x.webm x.mp4

Note, .webm is Google's movie format.

Clip a Video

ffmpeg -ss 00:10:05 -i input.mp4 -to 00:13:50 -c copy output.mp4

-ss time_off
Set the start time offset
-to time_stop
Record or transcode stop time

Find Out What's Inside a Video Container

“.mp4”, “.mkv” etc are Video/audio container formats. They package together video file, audio files, and other files, of various formats into 1 file.

To find out what's inside, use ffprobe, e.g.

ffprobe file.mkv

Example output:

~/web/xahmusic_org/music/i $ ffprobe xx.mkv
ffprobe version 3.4 Copyright (c) 2007-2017 the FFmpeg developers
  built with Apple LLVM version 9.0.0 (clang-900.0.37)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.4 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, matroska,webm, from 'xx.mkv':
  Metadata:
    COMPATIBLE_BRANDS: iso6avc1mp41
    MAJOR_BRAND     : dash
    MINOR_VERSION   : 0
    ENCODER         : Lavf57.83.100
  Duration: 00:05:04.90, start: 0.000000, bitrate: 1329 kb/s
    Stream #0:0: Video: h264 (Main), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)
    Metadata:
      HANDLER_NAME    : VideoHandler
      DURATION        : 00:05:04.871000000
    Stream #0:1(eng): Audio: vorbis, 44100 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:05:04.903000000
f140.m4a vs m4a diff 2020-01-03 78kkt
f140.m4a vs m4a diff 2020-01-03

Put a Opus Audio into a .Webm Container

ffmpeg -i x.opus -c:a copy x.webm

Add Opus Audio into Existing Webm

ffmpeg -i y.webm -i x.opus -c:v copy -c:a copy x.webm

Extract Audio from Video

extract audio file inside file x.mp4 and name it x.m4a

ffmpeg -i x.mp4 -vn -acodec copy x.m4a

ffmpeg -f mp4 -i x.mp4 -vn -c:a copy x.m4a

or

ffmpeg -f mp4 -i x.mp4 -map 0:1 -c:a copy x.m4a

-map 0:1 means stream number 1 from file number 0, which would usually be the audio track (edit as necessary). specifying a map at all disables the default map which would be to copy every stream, so you get only the audio, not both. -c:a copy means just copy it, no re-encoding. You may also want to specify a -f for the output to choose a container fomat; I am not sure what it will use by default, probably the same as the input.

2018-11-26 thanks to harvhat https://noagendasocial.com/@harvhat and matthew skala https://mstdn.io/web/accounts/18568

syntax of ffmpeg

The general syntax of ffmpeg is:

ffmpeg options infile_options -i infileName outfile_options outfileName

-f fmt
force format fmt

ffmpeg manual

Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Getting help:
    -h      -- print basic options
    -h long -- print more options
    -h full -- print all options (including all format and codec specific options, very long)
    -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf
    See man ffmpeg for detailed description of the options.

Print help / information / capabilities:
-L                  show license
-h topic            show help
-? topic            show help
-help topic         show help
--help topic        show help
-version            show version
-buildconf          show build configuration
-formats            show available formats
-muxers             show available muxers
-demuxers           show available demuxers
-devices            show available devices
-codecs             show available codecs
-decoders           show available decoders
-encoders           show available encoders
-bsfs               show available bit stream filters
-protocols          show available protocols
-filters            show available filters
-pix_fmts           show available pixel formats
-layouts            show standard channel layouts
-sample_fmts        show available audio sample formats
-colors             show available color names
-sources device     list sources of the input device
-sinks device       list sinks of the output device
-hwaccels           show available HW acceleration methods

Global options (affect whole program instead of just one file:
-loglevel loglevel  set logging level
-v loglevel         set logging level
-report             generate a report
-max_alloc bytes    set maximum size of a single allocated block
-y                  overwrite output files
-n                  never overwrite output files
-ignore_unknown     Ignore unknown stream types
-filter_threads     number of non-complex filter threads
-filter_complex_threads  number of threads for -filter_complex
-stats              print progress report during encoding
-max_error_rate maximum error rate  ratio of errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.
-bits_per_raw_sample number  set the number of bits per raw sample
-vol volume         change audio volume (256=normal)

Per-file main options:
-f fmt              force format
-c codec            codec name
-codec codec        codec name
-pre preset         preset name
-map_metadata outfile[,metadata]:infile[,metadata]  set metadata information of outfile from infile
-t duration         record or transcode "duration" seconds of audio/video
-to time_stop       record or transcode stop time
-fs limit_size      set the limit file size in bytes
-ss time_off        set the start time offset
-sseof time_off     set the start time offset relative to EOF
-seek_timestamp     enable/disable seeking by timestamp with -ss
-timestamp time     set the recording timestamp ('now' to set the current time)
-metadata string=string  add metadata
-program title=string:st=number...  add program with specified streams
-target type        specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad               audio pad
-frames number      set the number of frames to output
-filter filter_graph  set stream filtergraph
-filter_script filename  read stream filtergraph description from a file
-reinit_filter      reinit filtergraph on input parameter changes
-discard            discard
-disposition        disposition

Video options:
-vframes number     set the number of video frames to output
-r rate             set frame rate (Hz value, fraction or abbreviation)
-s size             set frame size (WxH or abbreviation)
-aspect aspect      set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number  set the number of bits per raw sample
-vn                 disable video
-vcodec codec       force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff  set initial TimeCode value.
-pass n             select the pass number (1 to 3)
-vf filter_graph    set video filters
-ab bitrate         audio bitrate (please use -b:a)
-b bitrate          video bitrate (please use -b:v)
-dn                 disable data

Audio options:
-aframes number     set the number of audio frames to output
-aq quality         set audio quality (codec-specific)
-ar rate            set audio sampling rate (in Hz)
-ac channels        set number of audio channels
-an                 disable audio
-acodec codec       force audio codec ('copy' to copy stream)
-vol volume         change audio volume (256=normal)
-af filter_graph    set audio filters

Subtitle options:
-s size             set frame size (WxH or abbreviation)
-sn                 disable subtitle
-scodec codec       force subtitle codec ('copy' to copy stream)
-stag fourcc/tag    force subtitle tag/fourcc
-fix_sub_duration   fix subtitles duration
-canvas_size size   set canvas size (WxH or abbreviation)
-spre preset        set the subtitle options to the indicated preset

Video by Command Line Tutorials

Image/Video Tutorial