The following is the GNU All-permissive License as recommended in https://www.gnu.org/licenses/license-recommendations.en.html
Copyright (C) 2024 Free Software Foundation sysadmin@fsf.org
Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty.
Contributions are welcome. See https://savannah.gnu.org/maintenance/fsf/.
Transcode videos
See also LP 2019 Streaming and brains gmg for more ffmpeg commands.
Remux to fix seeking in icecast recordings
mkdir -p remux
for i in *.webm; do echo "$i" && ffmpeg -i $i -vcodec copy -acodec copy remux/$i ; done
Remove exif
You might want to remove Exif metadata from the source file before you transcode, to avoid mentions of Apple software:
exiftool -all= source.mov
Scale cartoon
To scale to a cartoon to a 360 px tall H.264 video, with somewhat low quality:
ffmpeg -i input.mov -acodec aac -b:a 64k -vcodec h264 -vf scale=-1:360 -crf 25 -preset veryslow -tune animation output.mp4
To boost the quality, reduce the -crf 25
value. The default is 23
. To speed
up encoding, for testing purposes, use something like -preset medium
.
Drop the -vf scale=-1:360
part for the original video resolution.
To do similar, but for WebM:
ffmpeg -i input.mov -acodec libvorbis -b:a 64k -vcodec libvpx -vf scale=-1:360 -b:v 800k -preset veryslow output.webm
To do similar, but for ogv:
ffmpeg -i input.mov -acodec libvorbis -b:a 64k -vcodec libtheora -vf scale=-1:360 -b:v 1300k -preset veryslow output.ogv
Trim by timestamp
ffmpeg -i input-video.webm -ss 00:00:16 -to 00:33:25 -acodec copy -vcodec copy output-video.webm
Trim and correct audio delay
(https://www.geekyhacker.com/2020/05/17/synchronize-audio-and-video-with-ffmpeg/)
# correct audio ahead video (delay audio by 200ms)
ffmpeg -i in.webm -itsoffset 00:00:00.200 -i in.webm -ss 00:00:47 -to 00:38:08 -c:a copy -c:v copy -map 0:v:0 -map 1:a:0 out.webm
# correct audio behind video (skip audio by 200ms)
ffmpeg -i in.webm -itsoffset 00:00:00.200 -i in.webm -ss 00:00:47 -to 00:38:08 -c:a copy -c:v copy -map 0:a:0 -map 1:v:0 out.webm
Transcode to vp9
If you have time, use 2 pass encoding (note, needs testing):
The 1st command creates a log file in the current directory which is used in the 2nd command. Do not run this twice in parallell in the same directory, or it will screw up the log file!
ffmpeg -threads 0 -i INPUT.webm -g 192 -vcodec libvpx-vp9 -vf scale=-1:720 -max_muxing_queue_size 9999 -b:v 750K -pass 1 -an -f null /dev/null && \
ffmpeg -threads 0 -i INPUT.webm -g 192 -vcodec libvpx-vp9 -vf scale=-1:720 -max_muxing_queue_size 9999 -b:v 750K -pass 2 -c:a libvorbis -qscale:a 5 OUTPUT.webm
background:
https://trac.ffmpeg.org/wiki/Encode/VP9#speed
-g = from ffmpeg -h full, it is group of picture (GOP) size. Google says this is the number of frames per key frame.
If you don't have time, use one pass:
ffmpeg -threads 0 -i INPUT.webm -g 192 -vcodec libvpx-vp9 -vf scale=-1:720 -max_muxing_queue_size 9999 -c:a libvorbis -qscale:a 5 -ss 00:00:05 -to 00:03:09 OUTPUT.webm
Extract audio from video
Single video.
ffmpeg -i $i -vn -acodec copy $i.ogg
Batch.
mkdir -p /data/incoming/lp2022-audio/nointro/
cd /data/incoming/lp2022-video/jupiter
for i in *.webm; do echo "$i" && ffmpeg -i $i -vn -acodec copy /data/incoming/lp2022-audio/nointro/$i.ogg ; done
cd /data/incoming/lp2022-video/saturn
for i in *.webm; do echo "$i" && ffmpeg -i $i -vn -acodec copy /data/incoming/lp2022-audio/nointro/$i.ogg ; done
cd /data/incoming/lp2022-video/neptune
for i in *.webm; do echo "$i" && ffmpeg -i $i -vn -acodec copy /data/incoming/lp2022-audio/nointro/$i.ogg ; done
cd /data/incoming/lp2022-audio/nointro/
rename 's/.webm.ogg/.ogg/g' *
Campaigns may want to add an audio introduction.
Adding an introduction with sox
Syntax: sox short1.wav short2.wav short3.wav long.wav
mkdir -p /data/incoming/lp2022-audio/withintro/
for i in *.ogg; do echo "$i" && sox /data/incoming/gregf-audio-intro-lp2022-stereo.ogg $i /data/incoming/lp2022-audio/withintro/$i ; done
If you get the error sox FAIL sox: Input files must have the same # channels
, you may need to change one of the inputs from mono to stereo. This can be done with ffmpeg. See doc.
ffmpeg -i mono.ogg -ac 2 stereo.ogg
If you get the error sox FAIL formats: no handler for detected file type 'opus'
, you may need to compile sox.
Adding an introduction with ffmpeg (Needs testing)
Both files must have the same codec.
mkdir -p /data/incoming/lp2022-audio/withintro/
for i in *.ogg; do echo "$i" && ffmpeg -i /data/incoming/gregf-audio-intro-lp2022.ogg -i $i -filter_complex '[0:0][1:0]concat=n=2:v=0:a=1[out]' -map '[out]' /data/incoming/lp2022-audio/withintro/$i ; done
Combine videos with the same codec (needs testing)
for f in *.webm; do echo "file '$f'" >> mylist.txt; done
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.webm
Replace audio track
If an audio segment needs additional processing in audacity, you can bring it back in through ffmpeg.
ffmpeg -i video.webm -i audio.ogg -c:v copy -map 0:v:0 -map 1:a:0 new.webm