Transcode audio with metadata

ffmpeg and quodlibet/operon

Encode/compress raw video and audio

Compress raw video into Matroska container, see Digitizing Hi8 cassettes & Digitizing VHS cassettes.

Screencast on Linux with PulseAudio

Choose a PulseAudio source device

$ pactl list sources

To perform capture and lossless compression with x264 and Vorbis in one go, run:

With microphone

Close pavucontrol and run below to load loopback module

$ pactl load-module module-loopback latency_msec=1

Open pavucontrol again, and configure using below pictures as guide

Playback tab

Substitute below level to 70%

Recording tab

Substitute below level Built-in Audio Analog Stereo to 70%

Input Devices tab

Then repeat steps above.

Video to GIF

Manually extract portion of video into a video file adding extra stuff like effects, subtitles, merges with images, etc.

Resize video to desired size of GIF, using only one coordinate

$ ffmpeg -i video_source.mp4 -vf scale=-1:720 video_source_resized.mp4

If you get an error about audio encoding, just copy/reencode the same audio

$ ffmpeg -i video_source.mp4 -vf scale=-1:720 -c:a copy video_source_resized.mp4

File extension mp4 refers only to container, is not that relevant. Use whatever the original video extension is.

Finally, convert to GIF

$ ffmpeg -i video_source_resized.mp4 -b 2048k video.gif


Trim/cut video to video

$ in=/path/to/input/file
$ out=/path/to/output/file

Trim/cut from minute 18:20 to 18:30. Try following commands in descending order of preference/priority.

Fast without re-encoding

$ ffmpeg -i $in -ss 00:18:20 -c:v copy -c:a copy -to 00:18:30 $out

About as fast as previous, no re-encoding

$ ffmpeg -ss 00:18:20 -i $in -c:v copy -c:a copy -t 10 $out

Slower, more accurate, with re-encoding

$ ffmpeg -ss 00:18:20 -i $in -t 10 $out

Slower, CPU-intensive, more accurate, with re-encoding

$ ffmpeg -i $in -ss 00:18:20 -to 00:18:30 $out

Short Python script


  • Use H.264 because of its good quality, size and compatibility.

  • Use MP4 if using audio compression, otherwise use Matroska.

  • When dealing with raw audio (PCM) specify the format with the -f option (for example).

  • Don’t try to map audio channels without re-encoding (info).

Videos with wrong orientation in metadata

Sometimes when recording from a device like a cellphone or a tablet, the resulting video will play as if it was recorded in portrait mode

$ ffprobe VID_20160924_012917.mp4
ffprobe version 2.8.7 Copyright (c) 2007-2016 the FFmpeg developers
  built with gcc 5.3.1 (GCC) 20160406 (Red Hat 5.3.1-6)
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'VID_20160924_012917.mp4':
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-09-24 06:33:43
  Duration: 00:04:24.47, start: 0.000000, bitrate: 12234 kb/s
    Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080, 12130 kb/s, SAR 1:1 DAR 16:9, 30.33 fps, 30 tbr, 90k tbn, 180k tbc (default)
      rotate          : 90
      creation_time   : 2016-09-24 06:33:43
      handler_name    : VideoHandle
    Side data:
      displaymatrix: rotation of -90.00 degrees
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 96 kb/s (default)
      creation_time   : 2016-09-24 06:33:43
      handler_name    : SoundHandle

Below command fixes it without re-encoding.

$ ffmpeg -i VID_20160924_012917.mp4  -c copy -metadata:s:v:0 rotate=0 output.mp4

Map video and audio streams into single output

Merge the video stream of normal_export.mkv and audio stream of normal_export_Audio_1.mkv into a single container MKV normal_export_final.mkv.

$ ffmpeg -i normal_export.mkv  -i normal_export_Audio_1.mkv -map 0:0 -map 1:1 -c:v copy -c:a copy normal_export_final.mkv