#/bin/bash

set -x

# Calculate size
# find ./ -type f   \( -name "*.wmv" -o -name "*.WMV" -o -name "*.mp4" -o -name "*.MP4" \)   -exec du -ch {} +

# Find all
# find ./ -type f  ! \( -name "*.mp4" -o -name "*.zip" -o -name "*.chm" -o -name "*.CHM"  \)

# Find and remove
# find ./ -type f   \( -name "*.wmv" -o -name "*.WMV" \) -exec bash -c 'rm "$0"' {} \;

# Setup folder structure
# find ./season1-4 -type d -exec mkdir -p -- ./output/{} \;

# Probe all
# find ./ -type f -size +11M -name "*.wmv" -exec bash -c 'ffprobe $0' {} \;

# Rename all .WMV to .wmv
# find ./season1-4 -type f -size +1M -name "*.WMV" -exec bash -c 'mv "$1" "${1%.WMV}".wmv' _ {} \;

# VAAPI HEVC
#find ./season1-4 -type f -size +1M \( -name "*.wmv" -o -name "*.mp4" \) -exec bash -c '  
#    ffmpeg                                                                                                            \
#        -init_hw_device vaapi=foo:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_device foo -hwaccel_output_format vaapi \
#        -y -i $0 -vf "format=nv12|vaapi,hwupload"                                                                     \
#        -c:v hevc_vaapi -qp:v 21 -profile:v main -level 4                                                             \
#        -c:a aac -q:a 100                                                                                             \
#        ./output/${0/wmv/mp4}                                                                                         \
#    ' {} \;

# VAAPI h264
#find ./season1-4 -type f -size +1M \( -name "*.wmv" -o -name "*.mp4" \) -exec bash -c '                               \
#    ffmpeg                                                                                                            \
#        -init_hw_device vaapi=foo:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_device foo -hwaccel_output_format vaapi \
#        -y -i $0 -vf "format=nv12|vaapi,hwupload"                                                                     \
#        -c:v h264_vaapi -qp:v 21 -profile:v main -level 4                                                             \
#        -c:a aac -q:a 100                                                                                             \
#        ./output/${0/wmv/mp4}                                                                                         \
#    ' {} \;

# Rename *.wmv.mp4 to *.mp4
# find ./ -name "*.wmv.mp4" -type f -exec bash -c 'echo "${1%.wmv.mp4}".mp4'  _ {} \;
# find ./ -name "*.wmv.mp4" -type f -exec bash -c 'echo "{}".mp4'  \;

# Run multiple ffmpeg simultaneously

# Convert whatever to h265
# Default CTU size = 64, which has an encode parallelization cap of 1080/64 = 16 threads, if it's 1080p content
# For weidongshan's content, mostly it's 768p. So 768/64 = 12 threads. This PC has 32 threads so 3 parallel task.
# https://kokomins.wordpress.com/2019/10/10/anime-encoding-guide-for-x265-and-why-to-never-use-flac/#what-preset-should-i-use
# x265's CRF=28 is as good as x264's CRF=23. Same quality, half the size
find ./coder_cow -type f -size +1M \( -name "*.wmv" -o -name "*.mp4" \) -print0     \
    |xargs -n1 -P2 -I{} -0 -exec bash -x -c    \
    '   \
    mkdir -p "./output/${0%/*}";  \   #Find last '/' and ignore the following
    ffmpeg -y -i "$0" -c:v libx265 -crf 28 -preset slow  -c:a aac -q:a 100 "./output/${0/wmv/mp4}";  \
    if [[ $? -ne 0 ]]; then
        echo "failed with: ${0}\n" > ./output/failed_files;
#        exit 255;
    fi                       
    ' {} \;

# Convert whatever to with intel HW acceleration
#find ./season1-4 -type f -size +1M -name "*.mp4" -print0 |                                                            \
#    xargs -n1 -P1 -I{} -0                                                                                             \
#    ffmpeg                                                                                                            \
#        -init_hw_device vaapi=foo:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_device foo -hwaccel_output_format vaapi \
#        -y -i {}                                                                                                      \
#        -vf 'format=nv12|vaapi,hwupload'                                                                              \
##        -c:v libx265 -crf 23 -preset slow                                                                            \
#        -c:v hevc_vaapi -qp:v 21 -profile:v main -level 4                                                             \
#        -c:a aac -q:a 100 ./output/{}

# Convert wma to mp4 & h265
# find ./season1-4 -type f -size +1M -name "*.wmv" -print0 |xargs -n1 -P2 -I{} -0 bash -c 'ffmpeg -y -i "${1}" -c:v libx265 -crf 23 -preset slow -c:a aac -q:a 100 ./output/${1%.wmv}.mp4' _ {}

# Search for 'x264 slow King is dead, long live the x264 faster'
# find ./ -type f -size +11M -name "*.wmv" -exec bash -c 'ffmpeg -y -i $0 -c:v libx264 -crf 23 -preset faster -c:a aac -q:a 100 ${0/wmv/mp4}' {} \;

# Remvoe all
# find ./output/ -type f -exec rm {} \;
