#!/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!

_report(){
    local RED="$(tput setaf 1)"    # Red      - For Warnings
    local GREEN="$(tput setaf 2)"  # Green    - For Declarations
    local BLUE="$(tput setaf 4)"   # Blue     - For Questions
    local NC="$(tput sgr0)"        # No Color
    local COLOR=""
    local STARTMESSAGE=()
    local ECHOOPT=""
    OPTIND=1
    while getopts "qdwstn" opt ; do
        case "${opt}" in
            q) COLOR="${BLUE}" ;;                        # question mode, use color blue
            d) COLOR="${GREEN}" ;;                       # declaration mode, use color green
            w) COLOR="${RED}" ;;                         # warning mode, use color red
            s) STARTMESSAGE+=([${SCRIPTNAME}] ) ;;       # prepend scriptname to the message
            n) ECHOOPT="-n" ;;                           # to avoid line breaks after echo
        esac
    done
    shift "$((OPTIND-1))"
    MESSAGE="${1}"
    echo ${ECHOOPT} "${COLOR}${STARTMESSAGE[@]}${MESSAGE}${NC}"
}

outputdir="samples"

if [[ -n "${DVRESCUE_FONT}" ]] ; then
  DEFAULTFONT="${DVRESCUE_FONT}"
elif [[ -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/mipops/dvrescue/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
https://github.com/mipops/dvrescue/files/5008881/O_O.dv.zip
)

if [[ ! -d samples ]] ; then
  mkdir samples
fi
for i in "${online_samples[@]}" ; do
  NAME="$(basename "${i}")"
  if [[ ! -f "samples/${NAME//.zip}" ]] ; then
    cd samples
    echo "downloading ${NAME}..."
    curl -L -O "${i}"
    EXT="${i##*.}"
    echo "ZIP $EXT"
    if [[ "${EXT}" == "zip" ]] ; then
      unzip "${NAME}"
    fi
    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'" -metadata timecode="1:00:00:00" 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'" -metadata timecode="2:00:00:00" 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'" -metadata timecode="3:00:00:00" 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'" -metadata timecode="4:00:00:00" 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'" -metadata timecode="5:00:00:00" 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'" -metadata timecode="6:00:00:00" 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'" -metadata timecode="7:00:00:00" samples/dv50_pal__422_4-3_2ch_48k_bars_sine.dv

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

