Saturday, April 24, 2021

Handy ffmpeg Cheatsheet

First convert the subtitles to .ass format:
ffmpeg -i sub.ass
Then add them using a video filter:

ffmpeg -i in.mp4 -vf ass=sub.ass out.mp4

## Extract the frames from a video

To extract all frames from between 1 and 5 seconds, and also between 11 and 15 seconds:

ffmpeg -i in.mp4 -vf select='between(t,1,5)+between(t,11,15)' -vsync 0 out%d.png

To extract one frame per second only:

ffmpeg -i in.mp4 -fps=1 -vsync 0 out%d.png

## Rotate a video

Rotate 90 clockwise:

ffmpeg -i -vf "transpose=1"

For the transpose parameter you can pass:

0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip

Use `-vf "transpose=2,transpose=2"` for 180 degrees.

## Download "Transport Stream" video streams

1. Locate the playlist file, e.g. using Chrome > F12 > Network > Filter: m3u8
2. Download and concatenate the video fragments:

ffmpeg -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4

If you get a "Protocol 'https not on whitelist 'file,crypto'!" error, add the `protocol_whitelist` option:

ffmpeg -protocol_whitelist "file,http,https,tcp,tls" -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4

## Mute some of the audio

To replace the first 90 seconds of audio with silence:

ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='lte(t,90)':volume=0" out.mp4

To replace all audio between 1'20" and 1'30" with silence:

ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='between(t,80,90)':volume=0" out.mp4

## Deinterlace

Deinterlacing using "yet another deinterlacing filter".

ffmpeg -i in.mp4 -vf yadif out.mp4

## Create a video slideshow from images

Parameters: `-r` marks the image framerate (inverse time of each image); `-vf fps=25` marks the true framerate of the output.

ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4

## Extract images from a video

- Extract all frames: `ffmpeg -i input.mp4 thumb%04d.jpg -hide_banner`
- Extract a frame each second: `ffmpeg -i input.mp4 -vf fps=1 thumb%04d.jpg -hide_banner`
- Extract only one frame: `ffmpeg -i input.mp4 -ss 00:00:10.000 -vframes 1 thumb.jpg`

## Metadata: Change the title

ffmpeg -i in.mp4 -map_metadata -1 -metadata title="My Title" -c:v copy -c:a copy out.mp4

Sunday, April 18, 2021

Stripping audio from video

 Handy ffmpeg command to strip audio from video with high quality:

ffmpeg -i ../video/$i -q:a 0 -map a $i.mp3

Tuesday, December 22, 2020

Stripping spaces from filenames

 Here is a handy script for stripping spaces from filenames in a directory and all of its subdirectories:


function strip_spaces_from_files {

  for i in *


    newfilename=$(echo $i | tr -s ' ' | tr ' ' '_')

    if [ -e "$newfilename" ]


      echo "$newfilename does not contain any spaces"


      echo "stripping spaces from $newfilename"

      mv "$i" "$newfilename"





echo "Stripping spaces from current directory $INITIALDIR"


echo "Stripping spaces from sub directories"

for subd in $(find ./ -type d)


  strippingdot=$(echo "${subd}"|sed 's/^..//')

  echo "switching to directory $INITIALDIR/$strippingdot"

  cd $INITIALDIR/$strippingdot

  echo "stripping spaces"


  echo "going back to initial directory $INITIALDIR"



Friday, December 11, 2020

Validating yaml with python

 python -c 'import yaml, sys; yaml.safe_load(sys.stdin)' < url-monitor-extension-config.yml

Thursday, November 26, 2020

Python introspection blog post

 A great blog post about how to use python modules to inspect python code itself can be found here

Thursday, April 16, 2020

Veiryfing ssh fingerprints of a system

When trying to connect to a system via ssh for the first time you will get a message that you need to trust the ssh fingerprint of that system.

How do you know if the fingerprint displayed is the correct one, or if there is a man in the middle attack going on?

If you have another terminal open on the system you have you can go to /etc/ssh and execute the following:

for file in *
do   ssh-keygen -lf $file

This will diplay the ssh fingerprints valid for this system so you can double check them.

Sunday, March 8, 2020

Enabling multiple displays with Thinkpad P52

When switching to power save mode nvidia disables the graphics card multiple display support.

It does not enable it back when switching to performance mode. This was done by creating a file in /lib/modprobe.d/nvidia-kms.conf and setting modeset to 1.

We can revert this change by changing the modeset to 0 or commenting this line. After rebooting, the external display works again.