Skip to content

Instantly share code, notes, and snippets.

@reticivis-net
Created January 27, 2022 03:18
Show Gist options
  • Save reticivis-net/653c5928d480daca6de23eb139f00df4 to your computer and use it in GitHub Desktop.
Save reticivis-net/653c5928d480daca6de23eb139f00df4 to your computer and use it in GitHub Desktop.
simple python script that uses FFmpeg to perform databending
import fractions
import subprocess
import json
# input settings
infile = "in.gif"
outfile = "out.mp4"
# databend settings
effect = "biquad"
pix_fmt = "yuv420p"
# convert from input to raw video
p = subprocess.Popen(["ffmpeg", "-hide_banner", "-y", "-i", infile, "-pix_fmt", pix_fmt, "-f", "rawvideo", "pipe:1"],
stdout=subprocess.PIPE)
rawvideo = p.communicate()[0]
# get info from original to reconstruct later
p = subprocess.Popen(['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', infile],
stdout=subprocess.PIPE)
infoprobe = p.communicate()[0]
info = json.loads(infoprobe)
fps = float(fractions.Fraction(info["streams"][0]["r_frame_rate"]))
width = info["streams"][0]["width"]
height = info["streams"][0]["height"]
# apply effect to audio
p = subprocess.Popen(['ffmpeg', '-hide_banner', '-y', '-f', 'alaw', '-acodec', 'pcm_alaw',
'-i', 'pipe:0', "-af", effect, '-f', 'alaw', "pipe:1"], stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
alaw = p.communicate(input=rawvideo)[0][:len(rawvideo)]
# convert audio back to original
p = subprocess.Popen(['ffmpeg', '-hide_banner', '-y', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-s',
f"{width}x{height}", '-r', str(fps),
'-pix_fmt', pix_fmt, '-i', 'pipe:0', outfile], stdin=subprocess.PIPE)
p.communicate(input=alaw)
# play it
subprocess.Popen(["ffplay", "-loop", "0", "-y", "750", outfile])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment