#! /usr/bin/env bash

# before running this script, please read the full script

# download the video file (see "v0" below)
# then run this script to produce a muxed video

set -e
set -x

# Network.German.REMASTERED.1976.AC3.BDRip.x264-SPiCY
# 23.976fps
a1=Network.1976.DL.English.German.720p.x264.de.23976fpms.ac3

# see also
# https://mygully.com/showthread.php?t=4638034
# Network.1976.German.AC3D.720p.BluRay.x264-RB

function run_ffmpeg() {
  if [ -e "$o" ]; then
    echo "output exists: $o"
    return
  fi
  ffmpeg -i "$v0" -ss "$a1_ss" -itsoffset $a1_itsoffset -i "$a1" -c copy -map_metadata 0 \
    -map 0:v:0 \
    -map 0:a:0 -metadata:s:a:0 language=eng \
    -map 1:a:0 -metadata:s:a:1 language=ger \
    "$o"
  echo "done $o"
}

# more seeders, 23.810fps, x264
# magnet:?xt=urn:btih:89e568f5bd7a4ebededcc7871830b3023e86b0ca
v0='../Network (1976) 720p BluRay.x264 SUJAIDR/Network (1976) 720p BluRay.x264 SUJAIDR.mkv'
v0_size=1105437187
# skip the "studiocanal" into of the german version
a1_itsoffset=2.000
a1_ss=21.060
o=Network.1976.DL.English.German.720p.x264.SUJAIDR.mkv
if [ -e "$v0" ] && [ "$(stat -L -c%s "$v0")" = "$v0_size" ]; then
  run_ffmpeg
fi