#!/bin/bash
# this script generates various samples of dv edge cases

# this section focuses on changes in characteristics within a stream, replicating scenarios such as someone recording on a dv camcorder, then stopping, then changes the configuration of the camera, and then continuing to record. Oh, it happens!

outputdir="samples"

if [[ -f "/Library/Fonts/Andale Mono.ttf" ]] ; then
  DEFAULTFONT="/Library/Fonts/Andale Mono.ttf"
elif [[ -f "/System/Library/Fonts/Supplemental/Andale Mono.ttf" ]] ; then
  DEFAULTFONT="/System/Library/Fonts/Supplemental/Andale Mono.ttf"
elif [[ -f "/System/Library/Fonts/Monaco.dfont" ]] ; then
  DEFAULTFONT="/System/Library/Fonts/Monaco.dfont"
elif [[ -f "/Library/Fonts/Microsoft/Lucida Console.ttf" ]] ; then
  DEFAULTFONT="/Library/Fonts/Microsoft/Lucida\Console.ttf"
elif [[ -f "/Library/Fonts/LetterGothicStd.otf" ]] ; then
  DEFAULTFONT="/Library/Fonts/LetterGothicStd.otf"
else
  _report -wt "$(basename "${0}") can't find a preferred font to use, please report this error to https://github.com/amiaopensource/vrecord/issues"
fi

# sources

online_samples=(
http://samples.ffmpeg.org/archive/all/dv+dvvideo+pcm_s16le++dropout.dv
http://samples.ffmpeg.org/archive/all/dv+dvvideo+pcm_s16le++imovie_dv_file_with_dup_audio_streams_segfaults_libfaac.dv
http://samples.ffmpeg.org/archive/all/dv+dvvideo+pcm_s16le++mplayer-ppc-dv-bugs-mpeg-ps.dv
http://samples.ffmpeg.org/archive/all/dv+dvvideo+pcm_s16le++mplayer-ppc-dv-bugs-seek_failed.dv
http://samples.ffmpeg.org/DV-raw/shots0000.dv
http://samples.ffmpeg.org/DV-raw/small_test2.dv
http://samples.ffmpeg.org/DV-raw/voxnews.dv
http://samples.ffmpeg.org/ffmpeg-bugs/roundup/issue1407/dv_dsf_1_stype_1.dv
http://samples.ffmpeg.org/ffmpeg-bugs/roundup/issue1582/testfile.dv
http://samples.ffmpeg.org/ffmpeg-bugs/roundup/issue729/crash_ffplay.dv
http://samples.ffmpeg.org/ffmpeg-bugs/trac/ticket2340/error.dv
http://samples.ffmpeg.org/ffmpeg-bugs/trac/ticket2341/error_no_audio_detected.dv
http://samples.ffmpeg.org/mplayer-bugs/bug1322/infiniteloop.dv
http://samples.ffmpeg.org/V-codecs/DVSD/pond.dv
https://makeinstallnotwar.org/video/Demo_DV_720x480_CC.mov
)

if [[ ! -d samples ]] ; then
  mkdir samples
fi
for i in "${online_samples[@]}" ; do
  if [[ ! -f "samples/$(basename "${i}")" ]] ; then
    cd samples
    echo "downloading $(basename "${i}")..."
    curl -O "${i}"
    cd -
  fi
done

echo "generating more dv files..."
DRAWTEXT_OPTS="fontfile=${DEFAULTFONT}:x=(w-text_w)/2:y=(h-text_h)/2:fontsize=36:fontcolor=white:box=1:boxcolor=gray@0.6:boxborderw=12:shadowx=2:shadowy=2:expansion=none"

ffmpeg -y -f lavfi -i testsrc2=r=30000/1001:s=720x480 -f lavfi -i sine -pix_fmt yuv411p -c:v dvvideo -aspect 4/3  -t 2 -ac 2 -ar 48000 -vf "drawtext=${DRAWTEXT_OPTS}:text='DV25 NTSC 4\:1\:1 4\3  2Ch 48kHz'" samples/dv25_ntsc_411_4-3_2ch_48k_bars_sine.dv
ffmpeg -y -f lavfi -i testsrc2=r=30000/1001:s=720x480 -f lavfi -i sine -pix_fmt yuv411p -c:v dvvideo -aspect 16/9 -t 2 -ac 2 -ar 48000 -vf "drawtext=${DRAWTEXT_OPTS}:text='DV25 NTSC 4\:1\:1 16\9 2Ch 48kHz'" samples/dv25_ntsc_411_169_2ch_48k_bars_sine.dv
ffmpeg -y -f lavfi -i testsrc2=r=25:s=720x576         -f lavfi -i sine -pix_fmt yuv411p -c:v dvvideo -aspect 4/3  -t 2 -ac 2 -ar 48000 -vf "drawtext=${DRAWTEXT_OPTS}:text='DV25 PAL  4\:1\:1 4\3  2Ch 48kHz'" samples/dv25_pal__411_4-3_2ch_48k_bars_sine.dv
ffmpeg -y -f lavfi -i testsrc2=r=25:s=720x576         -f lavfi -i sine -pix_fmt yuv411p -c:v dvvideo -aspect 4/3  -t 2 -ac 2 -ar 32000 -vf "drawtext=${DRAWTEXT_OPTS}:text='DV25 PAL  4\:1\:1 4\3  2Ch 32kHz'" samples/dv25_pal__411_4-3_2ch_32k_bars_sine.dv
ffmpeg -y -f lavfi -i testsrc2=r=25:s=720x576         -f lavfi -i sine -pix_fmt yuv420p -c:v dvvideo -aspect 4/3  -t 2 -ac 2 -ar 48000 -vf "drawtext=${DRAWTEXT_OPTS}:text='DV25 PAL  4\:2\:0 4\3  2Ch 48kHz'" samples/dv25_pal__420_4-3_2ch_48k_bars_sine.dv
ffmpeg -y -f lavfi -i testsrc2=r=30000/1001:s=720x480 -f lavfi -i sine -pix_fmt yuv422p -c:v dvvideo -aspect 4/3  -t 2 -ac 2 -ar 48000 -vf "drawtext=${DRAWTEXT_OPTS}:text='DV50 NTSC 4\:2\:2 4\3  2Ch 48kHz'" samples/dv50_ntsc_422_4-3_2ch_48k_bars_sine.dv
ffmpeg -y -f lavfi -i testsrc2=r=25:s=720x576         -f lavfi -i sine -pix_fmt yuv422p -c:v dvvideo -aspect 4/3  -t 2 -ac 2 -ar 48000 -vf "drawtext=${DRAWTEXT_OPTS}:text='DV50 PAL  4\:2\:2 4\3  2Ch 48kHz'" samples/dv50_pal__422_4-3_2ch_48k_bars_sine.dv

cat samples/dv*k_bars_sine.dv > samples/mix.dv

