Transcode audio with metadata

ffmpeg and quodlibet/operon

for src in *flac; do
dst="$(echo $src | sed -e 's/\.flac//' -e 's/ /_/g')".mp3
ffmpeg -i "$src" -ab 320k "$dst"
operon clear -a "$dst"
operon copy "$src" "$dst"
done

Encode/compress raw video and audio

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

date >> log.txt
echo 'Starting job' >> log.txt
sudo systemctl stop fahclient.service
sudo systemctl status --full fahclient.service
for src in *avi ; do
date >> log.txt ;
echo "Starting conversion for $src" >> log.txt ;
dst="$(echo $src | sed -e 's/\.avi$//')".mkv ;
ffmpeg -ac 2 -channel_layout stereo -i "$src" -c:v libx264 -preset slow -crf 17 -pix_fmt yuv422p -c:a pcm_s16le "$dst" ;
date >> log.txt ;
echo "Finished conversion, created $dst" >> log.txt ;
done
sudo systemctl start fahclient.service
sudo systemctl status --full fahclient.service
date >> log.txt
echo 'Ended job' >> log.txt

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:

#!/bin/sh -
# Example values
pulseaudio_source=0
thread_queue_size=32
video_size=1920x1080
framerate=30
record_area=$DISPLAY+0,0
naudio_channels=2
outfile=capture.mkv
# Command
ffmpeg -thread_queue_size $thread_queue_size -video_size $video_size \
-framerate $framerate -f x11grab -i $record_area \
-thread_queue_size $thread_queue_size -f pulse -ac $naudio_channels \
-channel_layout stereo -i $pulseaudio_source -c:v libx264 -qp 0 \
-preset ultrafast -c:a libvorbis $outfile

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

Online

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

from datetime import timedelta
from subprocess import Popen
'''ffmpeg_trim_cut_video2video.py
Edit section *User data*, and uncomment one of the commands `cmd`
'''
# User data
tstart = timedelta(minutes=18, seconds=20, milliseconds=0)
tend = timedelta(minutes=18, seconds=30, milliseconds=0)
src = 'video_src.mkv'
output = 'output.mkv'
ffmpeg = 'ffmpeg'
tdelta = tend - tstart
tstart = str(tstart)
tend = str(tend)
tdelta = str(tdelta.total_seconds())
cmd = (ffmpeg, '-i', src, '-ss', tstart, '-c:v', 'copy', '-c:a', 'copy', '-to',
tend, output)
#cmd = (ffmpeg, '-ss', tstart, '-i', src, '-c:v', 'copy', '-c:a', 'copy', '-t',
# tdelta, output)
#cmd = (ffmpeg, '-ss', tstart, '-i', src, '-t', tdelta, output)
#cmd = (ffmpeg, '-i', src, '-ss', tstart, '-to', tend, output)
proc = Popen(cmd)
print('Exit code: {}'.format(proc.wait()))

Misc

  • 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':
  Metadata:
    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)
    Metadata:
      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)
    Metadata:
      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

References

  1. How to Install FFmpeg on Windows, Mac, Linux Ubuntu and Debian
  2. https://trac.ffmpeg.org/wiki/AudioChannelManipulation
  3. https://trac.ffmpeg.org/wiki/audio%20types
  4. https://wiki.archlinux.org/index.php/FFmpeg
  5. https://trac.ffmpeg.org/wiki/Encode/H.264
  6. https://trac.ffmpeg.org/wiki/Capture/Desktop
  7. https://ffmpeg.org/ffmpeg-devices.html
  8. http://wiki.oz9aec.net/index.php/High_quality_screen_capture_with_Ffmpeg
  9. https://askubuntu.com/questions/123798/how-to-hear-my-voice-in-speakers-with-a-mic
  10. http://blog.superuser.com/2012/02/24/ffmpeg-the-ultimate-video-and-audio-manipulation-tool/
  11. https://trac.ffmpeg.org/wiki