Skip to content

Instantly share code, notes, and snippets.

@lordastley
Last active March 1, 2024 09:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lordastley/5127027 to your computer and use it in GitHub Desktop.
Save lordastley/5127027 to your computer and use it in GitHub Desktop.
1-bit dithering a video
ffmpeg -i input.mp4 -s hd480 -f image2 img-%4d.png # dump the video to .png images, 480p
for i in *.png; do convert $i -colorspace Rec709Luma pgm:- | pamditherbw -atkinson | pnmtopng > p$i; done; # convert .png images to 1-bit dithered .pngs
ffmpeg -r [orig. framerate] -i pimg-%4d.png -i input.mp4 -map 0:0 -map 1:1 -c:a copy -c:v libx264 -tune stillimage -crf 18 -r 24 lol.mkv
#take dithered png files, original video's audio, make a video with x264, tuned for still images and a copy of the audio. specify frame rate for proper a/v sync
# -map 1:1 maps audio from 2nd input (orig video) to audio of new video. assumes stream 1 is desired audio track. check this.
# notes:
%4d = 4 digits. increase for longer video clips
doing gamma correction / adjustment is a very good idea. will reduce the number of frames that are solid black or solid white
-colorspace Rec709Luma
is probably the better grayscale conversion option - based on luminance from ITU-R BT.709 (see also http://www.imagemagick.org/script/command-line-options.php#colorspace)
Other greyscale colorspace options:
-colorspace Rec601Luma = -colorspace Gray
#mjpeg video
ffmpeg -r [orig. framerate] -i pimg-%4d.png -i input.mp4 -map 0:0 -map 1:1 -acodec copy -vcodec mjpeg -q:v 0 -r [original framerate] lol.mkv
#take dithered png files, original video's audio, make a video with MJPEG'd images and a copy of the audio. specify frame rate for proper a/v sync
# assumes stream 1 is desired audio track. check this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment