#!/usr/bin/env bash

set -eu

tmpext="_mp4-concat.tmp.ts"
TMP_FILES=()

for f in "$@"; do
  TMP_FILES+=("$f$tmpext")
  ffmpeg -i "$f" -c copy -bsf:v h264_mp4toannexb -f mpegts "$f$tmpext"
done

concat_files=$(printf "|%s" "${TMP_FILES[@]}")
concat_files=${concat_files:1}
out_file="mp4concat_${RANDOM}.mp4"

ffmpeg -i "concat:$concat_files" -c copy -bsf:a aac_adtstoasc "$out_file"

for f in "${TMP_FILES[@]}"; do
  rm "$f"
done

strip-metadata "$out_file"