Skip to content

Instantly share code, notes, and snippets.

@gasman
Created October 25, 2020 12:23
Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save gasman/1253b764049cfab3e29739d3f217c9c6 to your computer and use it in GitHub Desktop.
Save gasman/1253b764049cfab3e29739d3f217c9c6 to your computer and use it in GitHub Desktop.
Encoding a file as a Youtube video - https://www.youtube.com/watch?v=hyqLv2_zBdA
# Encode inputfile.tar.gz as a series of video frames
# Frames are written to frames/frameNNNN.png
from PIL import Image
with open('inputfile.tar.gz', 'rb') as f:
data = f.read()
WIDTH = 120
HEIGHT = 90
CHUNK_SIZE = int((WIDTH * HEIGHT) / 8)
chunks = []
offset = 0
while offset < len(data):
chunks.append(data[offset:offset+CHUNK_SIZE])
offset += CHUNK_SIZE
for frame_num, chunk in enumerate(chunks):
padded_chunk = chunk + (b'\0' * (CHUNK_SIZE - len(chunk)))
img = Image.frombytes('1', (WIDTH, HEIGHT), padded_chunk)
img.save('frames/frame%04d.png' % (frame_num + 1))
# Encode as video with:
# ffmpeg -i frames/frame%04d.png -vf scale=1440:1080 -sws_flags neighbor -vcodec ffv1 output.mkv
# Decode the resulting downloaded video with:
# ffmpeg -i downloaded.mkv -vf scale=120:-1,eq=contrast=10 -sws_flags neighbor -pix_fmt monob -f rawvideo result.tar.gz
@stefanbosch
Copy link

This is fun to play with! 👍 Thanks for sharing

@SasquatchYuja
Copy link

good occasion to mention http://decss.zoy.org

@setanarut
Copy link

I'm trying to improve this idea;
https://github.com/hazarek/fif

@fernandoapparte
Copy link

After few testing this will work fine with 160x90, and changing encoding decoding:
ffmpeg -i frames/frame%04d.png -vf scale=1920:1080 -sws_flags neighbor -vcodec ffv1 output.mkv
ffmpeg -i downloaded.mkv -vf scale=160:-1,eq=contrast=10 -sws_flags neighbor -pix_fmt monob -f rawvideo result.tar.gz

@setanarut
Copy link

setanarut commented Dec 7, 2020

Scaling up video is a smart way to bypass YouTube video compression, but this causes the file size to increase. Vimeo supports lossless video. The raw MOV video might work. Also video is not a good choice for files up to 50 mb (approximately).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment