#!/bin/bash
# Builds the two edge files, then runs the two published commands on the test file (lean-hevc.mp4) and on an edge file; writes commands.log. in.mp4 on the page is the reader's file.
cd "$(dirname "$0")"
W=$(mktemp -d); trap "rm -rf $W" EXIT
# edge 1: Matroska remux of the test file (stream bit rate is not stored); edge 2: H.264 video with PCM audio in .mov (PCM cannot go in .mp4)
ffmpeg -v error -y -i lean-hevc.mp4 -c copy edge.mkv
ffmpeg -v error -y -f lavfi -i "sine=frequency=440:duration=3:sample_rate=48000" -ac 2 -c:a pcm_s16le $W/a.wav
ffmpeg -v error -y -i src.mp4 -i $W/a.wav -map 0:v -map 1:a -c:v libx264 -preset medium -crf 30 -c:a pcm_s16le -t 3 edge-pcm.mov
: > commands.log
p1(){ echo "published: ffprobe -v error -show_entries format=duration,size,bit_rate:stream=codec_name,bit_rate -of default=noprint_wrappers=1 in.mp4" >> commands.log
  echo "run: ffprobe -v error -show_entries format=duration,size,bit_rate:stream=codec_name,bit_rate -of default=noprint_wrappers=1 $1" >> commands.log
  out=$(ffprobe -v error -show_entries format=duration,size,bit_rate:stream=codec_name,bit_rate -of default=noprint_wrappers=1 $1 2>&1); ec=$?
  echo "output: $(echo "$out" | tr '\n' ' ')" >> commands.log; echo "exit=$ec" >> commands.log; [ -n "$2" ] && echo "edge: $2" >> commands.log; echo >> commands.log; }
p2(){ echo "published: ffmpeg -i in.mp4 -c:v libx264 -b:v 1400k -c:a aac -b:a 128k out.mp4" >> commands.log
  echo "run: ffmpeg -v error -y -i $1 -c:v libx264 -b:v 1400k -c:a aac -b:a 128k $3" >> commands.log
  out=$(ffmpeg -v error -y -i $1 -c:v libx264 -b:v 1400k -c:a aac -b:a 128k $3 2>&1); ec=$?
  echo "output: in_bytes=$(stat -c %s $1) out_bytes=$(stat -c %s $3 2>/dev/null || echo none) ${out:+msg=$out}" >> commands.log; echo "exit=$ec" >> commands.log; [ -n "$2" ] && echo "edge: $2" >> commands.log; echo >> commands.log; rm -f $3; }
p1 lean-hevc.mp4 ""
p1 edge.mkv "Matroska file: the container stores no per-stream bit rate, so the stream line may show N/A while the format line still gives a figure; the test file is an .mp4"
p1 edge-pcm.mov "H.264 video with PCM audio in .mov; the test file has no audio stream, this one has two codec lines"
p2 lean-hevc.mp4 "" out.mp4
p2 edge-pcm.mov "input has PCM audio: -c:a aac converts it, so the output is smaller than the input; the test file has no audio" out.mp4
