#!/bin/bash

set -e

#Clean
rm -rfv /tmp/testsrc.mp4 /tmp/encoding.testsrc-OK.mp4 /tmp/encoding.testsrc-KO.mp4
rm -rfv /tmp/encoding.testsrc-OK.mp4.png /tmp/encoding.testsrc-KO.mp4.png

#Create samples
ffmpeg -loglevel error -f lavfi -i testsrc -vf "scale=1920:1080" -aspect 1920:1080 -t 10 -pix_fmt yuv420p /tmp/testsrc.mp4 -y

#Run ffmpeg 1
ffmpeg -i "/tmp/testsrc.mp4" -filter_complex '[0:v]copy[vr0]' -map '[vr0]' -vcodec libx264 -b:v:0 800K -profile:v baseline -pix_fmt yuv420p -preset "veryfast" -f mp4 "/tmp/encoding.testsrc-OK.mp4" -loglevel error -y

#Run ffmpeg 2
ffmpeg -i "/tmp/testsrc.mp4" -filter_complex '[0:v]copy[vr0]' -map '[vr0]' -vcodec libx264 -b:v:0 800K -profile:v baseline -pix_fmt yuv420p -preset "veryfast" -f tee "[select=\'v:0\':f=mp4]/tmp/encoding.testsrc-KO.mp4" -loglevel error -y

#Compare results
ffmpeg -loglevel error -ss 00:00:05 -i /tmp/encoding.testsrc-OK.mp4 -vframes 1 -q:v 0 /tmp/encoding.testsrc-OK.mp4.png -y
ffmpeg -loglevel error -ss 00:00:05 -i /tmp/encoding.testsrc-KO.mp4 -vframes 1 -q:v 0 /tmp/encoding.testsrc-KO.mp4.png -y
compare /tmp/encoding.testsrc-OK.mp4.png /tmp/encoding.testsrc-KO.mp4.png -compose src /tmp/diff.png || echo Files differ
