#!/bin/bash
if [ -z "$1" ]
then
	echo "Usage: $0 file[.usm]"
fi
root=$(dirname ${BASH_SOURCE-$0})
fn=$(basename $1 .usm)

$root/crid.exe -b "00000001" -a "43484A86" -o $fn.demux $fn.usm

au=$(ls $fn.demux/*.wav $fn.demux/*.mp3 $fn.demux/*.ogg $fn.demux/*.opus 2>/dev/null)
for i in $au
do
	a=$i
done
vi=$(ls $fn.demux/*.m2v $fn.demux/*.mp4 $fn.demux/*.mpg $fn.demux/*.webm $fn.demux/*.vp9 2>/dev/null)
for i in $vi
do
	v=$i
done

if [ -n "$a" ]
then aup="-i "$a
else aup="-f lavfi -i anullsrc"
fi

ffmpeg -loglevel warning -y $aup -i $v -shortest -c:v copy -c:a aac -b:a 320k $fn.ts

rm -r $fn.demux/
