import subprocess, sys
from pathlib import Path
for name in sys.argv[1:]:
    source=Path(name)
    target=source.with_name(source.stem+'-clean.mp4')
    decoder=subprocess.Popen(['ffmpeg','-v','error','-i',str(source),'-vf','fps=25','-f','rawvideo','-pix_fmt','rgb24','-'],stdout=subprocess.PIPE)
    encoder=subprocess.Popen(['ffmpeg','-v','error','-f','rawvideo','-pix_fmt','rgb24','-s','1280x500','-r','25','-i','-','-c:v','libx264','-pix_fmt','yuv420p','-movflags','+faststart',str(target)],stdin=subprocess.PIPE)
    size=1280*500*3
    while True:
        frame=decoder.stdout.read(size)
        if len(frame)!=size: break
        pixel=(20*1280+20)*3
        red,green,blue=frame[pixel:pixel+3]
        if blue>red+30:
            encoder.stdin.write(frame)
    encoder.stdin.close()
    if decoder.wait() or encoder.wait(): raise RuntimeError('Video conversion failed')
