From df579e60824809e5e1d32074796f62b6ec2005ea Mon Sep 17 00:00:00 2001 From: GenZmeY Date: Tue, 27 Oct 2020 02:25:40 +0300 Subject: [PATCH] init --- .gitignore | 3 + scripts/common.lib | 37 ++++++ scripts/extract.sh | 107 ++++++++++++++++++ scripts/frames.sh | 31 +++++ scripts/merge.sh | 35 ++++++ scripts/ranges.sh | 36 ++++++ scripts/returncodes.lib | 18 +++ scripts/test.sh | 2 + scripts/upscale.sh | 218 ++++++++++++++++++++++++++++++++++++ scripts/video.sh | 77 +++++++++++++ settings/ffmpeg.conf | 6 + settings/video2d-2x.conf | 9 ++ settings/waifu2x-caffe.conf | 14 +++ video2d-2x | 99 ++++++++++++++++ 14 files changed, 692 insertions(+) create mode 100644 .gitignore create mode 100644 scripts/common.lib create mode 100644 scripts/extract.sh create mode 100644 scripts/frames.sh create mode 100644 scripts/merge.sh create mode 100644 scripts/ranges.sh create mode 100644 scripts/returncodes.lib create mode 100644 scripts/test.sh create mode 100644 scripts/upscale.sh create mode 100644 scripts/video.sh create mode 100644 settings/ffmpeg.conf create mode 100644 settings/video2d-2x.conf create mode 100644 settings/waifu2x-caffe.conf create mode 100644 video2d-2x diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97ebdaf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +dependencies +input +output diff --git a/scripts/common.lib b/scripts/common.lib new file mode 100644 index 0000000..e4ad75a --- /dev/null +++ b/scripts/common.lib @@ -0,0 +1,37 @@ +#!/bin/bash + +# This file is part of video2d-2x. +# +# video2d-2x is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +function clean_line () # $1: Fill size +{ + printf "\r% $1s\r" "" +} + +function framerate () +{ + local StreamCount=$(jq -r '.format.nb_streams' "$FormatJson") + + for (( Index=0; Index < StreamCount; Index++ )) + do + if [[ $(jq -r ".streams[$Index].codec_type" "$StreamsJson") == "video" ]]; then + jq -r ".streams[$Index].r_frame_rate" "$StreamsJson" + return 0 + fi + done + + echo "ERR: No framerate info in $StreamsJson" + exit "$NO_INFO_ERROR" +} diff --git a/scripts/extract.sh b/scripts/extract.sh new file mode 100644 index 0000000..7478222 --- /dev/null +++ b/scripts/extract.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +# This file is part of video2d-2x. +# +# video2d-2x is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +function extension_by_codec () # $1: Codec +{ + # Where is my json?! + local Result=$( + ffprobe -v quiet -h muxer="$1" | \ + grep 'Common extensions:' | \ + sed -r 's|^.+: ([^,\.]+).+|\1|') + if [[ -z "$Result" ]]; then + Result=$( + ffprobe -v quiet -h demuxer="$1" | \ + grep 'Common extensions:' | \ + sed -r 's|^.+: ([^,\.]+).+|\1|') + fi + if [[ -n "$Result" ]]; then + echo "$Result" + else + echo "No extension for codec \"$1\"" + exit "$NO_EXTENSION_FOR_CODEC" + fi +} + +function extract_attachments () +{ + pushd "$AttachmentsDir" + + ffmpeg -hide_banner -dump_attachment:t "" -i "$InputFile" + + # There is no error checking, + # because ffmpeg always throws an error: + # "At least one output file must be specified", + # although it successfully saves attachments. + + popd +} + +function extract_chapters () +{ + # TODO: Convert $ChaptersJson to mkv-compatible format? + echo "DUMMY" +} + +if [[ -z "$1" ]]; then + echo "You must specify the video file" + exit "$PARAMETER_ERROR" +else + InputFile=$(readlink -e "$1") +fi + +if ! [[ -r "$InputFile" ]]; then + echo "Read file error: \"$InputFile\"" + exit "$FILE_READ_ERROR" +fi + +rm -rf "$AudioDir"; mkdir -p "$AudioDir" +rm -rf "$VideoDir"; mkdir -p "$VideoDir" +rm -rf "$SubtitlesDir"; mkdir -p "$SubtitlesDir" +rm -rf "$ChaptersDir"; mkdir -p "$ChaptersDir" +rm -rf "$AttachmentsDir"; mkdir -p "$AttachmentsDir" + +ffprobe -v quiet -print_format json -show_streams "$InputFile" > "$StreamsJson" +ffprobe -v quiet -print_format json -show_format "$InputFile" > "$FormatJson" +ffprobe -v quiet -print_format json -show_chapters "$InputFile" > "$ChaptersJson" + +StreamCount=$(jq -r '.format.nb_streams' "$FormatJson") + +for (( Index=0; Index < StreamCount; Index++ )) +do + Type=$(jq -r ".streams[$Index].codec_type" "$StreamsJson") + Codec=$(jq -r ".streams[$Index].codec_name" "$StreamsJson") + Extension=$(extension_by_codec "$Codec") + + case "$Type" in + video ) + ffmpeg -hide_banner -i "$InputFile" -map "0:$Index" -c:v copy "$VideoDir/$Index.$Extension" + if [[ "$?" != 0 ]]; then exit "$EXTRACT_AUDIO_ERROR"; fi ;; + audio ) + ffmpeg -hide_banner -i "$InputFile" -map "0:$Index" -c:a copy "$AudioDir/$Index.$Extension" + if [[ "$?" != 0 ]]; then exit "$EXTRACT_VIDEO_ERROR"; fi ;; + subtitle ) + ffmpeg -hide_banner -i "$InputFile" -map "0:$Index" "$SubtitlesDir/$Index.$Extension" + if [[ "$?" != 0 ]]; then exit "$EXTRACT_SUBTITLE_ERROR"; fi ;; + attachment ) + continue ;; + * ) + echo "Unknown codec type: \"$Type\"" + exit "$UNKNOWN_CODEC_TYPE_ERROR" ;; + esac +done + +extract_attachments diff --git a/scripts/frames.sh b/scripts/frames.sh new file mode 100644 index 0000000..3548668 --- /dev/null +++ b/scripts/frames.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# This file is part of video2d-2x. +# +# video2d-2x is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +InputFile=$(find "$VideoDir" -mindepth 1 -maxdepth 1 -type f | head -n 1) + +if ! [[ -r "$InputFile" ]]; then + echo "Read file error: \"$InputFile\"" + exit "$FILE_READ_ERROR" +fi + +rm -rf "$FramesDir"; mkdir -p "$FramesDir" + +ffmpeg -hide_banner -i "$InputFile" -r "$(framerate)" -f image2 "$FramesDir/%06d.png" + +if [[ "$?" != 0 ]]; then + exit "$CONVERT_TO_FRAMES_ERROR" +fi diff --git a/scripts/merge.sh b/scripts/merge.sh new file mode 100644 index 0000000..a2ed451 --- /dev/null +++ b/scripts/merge.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# This file is part of video2d-2x. +# +# video2d-2x is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# temporary solution. +# TODO: +# Replace with creating mkv container +# with all resources using mkvmerge + +rm -rf "$ReleaseDir"; mkdir -p "$ReleaseDir" + +ffmpeg \ + -hide_banner \ + -i $(find "$VideoUpscaledDir" -type f | head -n 1) \ + -i $(find "$AudioDir" -type f | head -n 1) \ + -vcodec "copy" \ + -acodec "copy" \ + "$ReleaseDir/release.mp4" + +if [[ $? -ne 0 ]]; then + exit "$MERGE_RELEASE_ERROR" +fi diff --git a/scripts/ranges.sh b/scripts/ranges.sh new file mode 100644 index 0000000..44a92f1 --- /dev/null +++ b/scripts/ranges.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# This file is part of video2d-2x. +# +# video2d-2x is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# TODO: auto-detect ranges +# compare adjacent frames using the duplicate image search algorithm +# frames that are unlike each other will be the boundaries of the ranges + +# Range list format: +# START_FRAME END_FRAME NOISE_LEVEL +# (separate line for each range) + +function add_range () # $1: Start frame, $2: End frame, $3: Noise level +{ + echo -e "$1\t$2\t$3" >> "$RangesList" +} + +StartFrame=$(ls "$FramesDir" | sort | head -n 1 | sed 's|.png$||') +EndFrame=$(ls "$FramesDir" | sort | tail -n 1 | sed 's|.png$||') +NoiseLevel="1" + +:> "$RangesList" +add_range "$StartFrame" "$EndFrame" "$NoiseLevel" diff --git a/scripts/returncodes.lib b/scripts/returncodes.lib new file mode 100644 index 0000000..27bc943 --- /dev/null +++ b/scripts/returncodes.lib @@ -0,0 +1,18 @@ +SUCCESS=0 +OPTION_ERROR=1 +PARAMETER_ERROR=2 +EXTRACT_AUDIO_ERROR=3 +EXTRACT_VIDEO_ERROR=4 +EXTRACT_SUBTITLE_ERROR=5 +EXTRACT_ATTACHMENT_ERROR=6 +UNKNOWN_CODEC_TYPE_ERROR=7 +NO_EXTENSION_FOR_CODEC=8 +CONVERT_TO_FRAMES_ERROR=9 +FILE_READ_ERROR=10 +RANGES_LIST_SYNTAX_ERROR=11 +SETTINGS_ERROR=12 +WAIFU2X_ERROR=13 +MERGE_RELEASE_ERROR=14 +NO_INFO_ERROR=15 +INTERRUPT=16 +CREATE_UPSCALED_VIDEO_ERROR=17 diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100644 index 0000000..05a7907 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/bash + diff --git a/scripts/upscale.sh b/scripts/upscale.sh new file mode 100644 index 0000000..02189ae --- /dev/null +++ b/scripts/upscale.sh @@ -0,0 +1,218 @@ +#!/bin/bash + +# This file is part of video2d-2x. +# +# video2d-2x is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +readonly TmpFramesDir="${FramesDir}_tmp" + +function create_default_conf () +{ + echo "\ +ScaleRatio=\"3\" +OutputDepth=\"16\" +Mode=\"noise_scale\" +CropSize=\"256\" +Model=\"upresnet10\" +" > "$Waifu2xConf" +} + +function to_int () # $1: String +{ + echo "$1" | \ + sed 's|.png||' | \ + sed -r 's|0*([1-9][0-9]*)|\1|' +} + +function set_range () # $@: Line +{ + StartFrame=$(to_int "$1") + EndFrame=$(to_int "$2") + NoiseLevel=$(to_int "$3") + + return $# +} + +function model_path () # $1: model name +{ + echo "$(dirname $(readlink -e $(which waifu2x-caffe-cui)))/models/$1" +} + +function upscale_images () # $1: InputDir, $2: OutputDir, $3: ProgressBarPID, $4: ParentPID +{ + waifu2x-caffe-cui \ + --scale_ratio "$ScaleRatio" \ + --output_depth "$OutputDepth" \ + --noise_level "$NoiseLevel" \ + --mode "$Mode" \ + --tta "$TtaMode" \ + --gpu "$GpuNum" \ + --process "$Process" \ + --crop_size "$CropSize" \ + --batch_size "$BatchSize" \ + --model_dir "$(model_path $Model)" \ + --input_path "$1" \ + --output_path "$2" \ + > /dev/null + local RT=$? + kill "$3" 2> /dev/null + if [[ "$RT" -ne 0 ]]; then + kill -1 "$4" 2> /dev/null + fi +} + +function check_ranges () +{ + local Errors=0 + local ParamCount=0 + local LineIndex=0 + local LastEndFrame="" + + while read Line + do + ((LineIndex++)) + set_range $Line + ParamCount=$? + if [[ "$ParamCount" -eq 0 ]]; then + continue + fi + if [[ "$ParamCount" -eq 3 ]]; then + if [[ "$StartFrame" =~ ^[0-9]+$ ]]; then + if [[ -n "$LastEndFrame" ]] && [[ $(($LastEndFrame+1)) != $StartFrame ]]; then + echo "ERR [$LineIndex]: StartFrame ($StartFrame) doesn't follow the previous one ($LastEndFrame)" + ((Errors++)) + fi + else + echo "ERR [$LineIndex]: StartFrame $StartFrame is not valid integer" + ((Errors++)) + fi + + if [[ "$EndFrame" =~ ^[0-9]+$ ]]; then + LastEndFrame="$EndFrame" + else + LastEndFrame="" + echo "ERR [$LineIndex]: EndFrame $EndFrame is not valid integer" + ((Errors++)) + fi + if [[ "$NoiseLevel" =~ ^[0-9]+$ ]]; then + if [[ "$NoiseLevel" -lt 0 ]] || [[ "$NoiseLevel" -gt 3 ]]; then + echo "ERR [$LineIndex]: NoiseLevel $NoiseLevel incorrect value (should be in the range 0-3)" + ((Errors++)) + fi + else + echo "ERR [$LineIndex]: NoiseLevel $NoiseLevel is not valid integer" + ((Errors++)) + fi + else + echo "ERR [$LineIndex]: $ParamCount parameters received (3 expected)" + ((Errors++)) + fi + done < <(cat "$RangesList"; echo) # make bash not skip the last line (if there is no empty line at the end) + if [[ "$Errors" -gt 0 ]]; then + echo "Ranges list syntax: $Errors errors" + fi + return "$Errors" +} + +function progress_bar () +{ + local PreviousUpscaledFrame="" + local LastUpscaledFrame="" + local Total=$(to_int $LastOriginalFrame) + while [[ "$LastUpscaledFrame" != "$LastOriginalFrame" ]] + do + LastUpscaledFrame=$(ls "$FramesUpscaledDir" | sort | tail -n 1) + if [[ "$PreviousUpscaledFrame" != "$LastUpscaledFrame" ]]; then + local Done=$(to_int $LastUpscaledFrame) + echo -ne "\r[$(printf "% 3d" $(($Done*100/$Total)))%] $Done/$Total" + PreviousUpscaledFrame="$LastUpscaledFrame" + fi + sleep 1 + done +} + +if ! [[ -e "$Waifu2xConf" ]]; then + create_default_conf +fi + +source "$Waifu2xConf" + +if ! check_ranges; then + exit "$RANGES_LIST_SYNTAX_ERROR" +fi + +rm -rf "$TmpFramesDir" +mkdir -p "$FramesUpscaledDir" + +if ! [[ -r "$RangesList" ]]; then + echo "Read file error: \"$RangesList\"" + exit "$FILE_READ_ERROR" +fi + +LastOriginalFrame=$(ls "$FramesDir" | sort | tail -n 1) +LastUpscaledFrame=$(ls "$FramesUpscaledDir" | sort | tail -n 1) + +if [[ "$LastUpscaledFrame" == "$LastOriginalFrame" ]]; then + echo "WARN: Upscaled frames already exists - skip." + exit "$SUCCESS" +fi + +LastUpscaledFrame=$(to_int "$LastUpscaledFrame") + +while read Line +do + set_range $Line + clean_line 32 + if [[ -n "$LastUpscaledFrame" ]] && [[ "$LastUpscaledFrame" -ge "$EndFrame" ]]; then + echo -e "\r$StartFrame - $EndFrame [$NoiseLevel] - SKIP" + continue + fi + + if [[ -n "$LastUpscaledFrame" ]] && [[ "$StartFrame" -lt "$LastUpscaledFrame" ]]; then + echo -e "\r$StartFrame ($LastUpscaledFrame) - $EndFrame [$NoiseLevel] - CONTINUE" + # if waifu2x-caffe was interrupted while saving the file, a corrupted file is saved + # so it's better to start by overwriting the last upscaled file + StartFrame="$LastUpscaledFrame" + else + echo -e "\r$StartFrame - $EndFrame [$NoiseLevel]" + fi + + rm -rf "$TmpFramesDir" + mkdir "$TmpFramesDir" + + echo -ne "\rCopying range..." + CopyList="" + for (( i=StartFrame; i <= EndFrame; i++)) + do + CopyList+="$(printf "%06d" $i).png " + done + + pushd "$FramesDir" > /dev/null + cp $CopyList "$TmpFramesDir" + popd > /dev/null + + clean_line 32 + + (progress_bar) & + ProgressBarPID=$! + + (upscale_images "$TmpFramesDir" "$FramesUpscaledDir" "$ProgressBarPID" "$$") & + Waifu2xPID=$! + + trap "kill $ProgressBarPID 2> /dev/null; exit $WAIFU2X_ERROR" HUP + trap "kill $Waifu2xPID $ProgressBarPID 2> /dev/null; echo -e '\nInterrupted'; exit $INTERRUPT" INT + wait 2> /dev/null + + rm -rf "$TmpFramesDir" +done < <(cat "$RangesList"; echo) # make bash not skip the last line (if there is no empty line at the end) diff --git a/scripts/video.sh b/scripts/video.sh new file mode 100644 index 0000000..5f15e32 --- /dev/null +++ b/scripts/video.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +# This file is part of video2d-2x. +# +# video2d-2x is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +function create_default_conf () +{ + echo "\ +VideoCodec=\"libx265\" +Preset=\"slow\" +PixelFormat=\"yuv420p10le\" +ConstantRateFactor=\"16\" +VideoBitrate=\"\" +x265params=\"\" +" > "$FfmpegConf" +} + +function auto_x265params () +{ + if [[ -n "$x265params" ]]; then + echo '-x265-params' "$x265params" + fi +} + +function auto_bitrate () +{ + if [[ -n "$ConstantRateFactor" ]]; then + echo '-crf' "$ConstantRateFactor" + elif [[ -n "$VideoBitrate" ]]; then + echo '-b:v' "$VideoBitrate" + else + echo "You must set ConstantRateFactor or VideoBitrate in ffmpeg.conf" + exit "$SETTINGS_ERROR" + fi +} + +if ! [[ -e "$FfmpegConf" ]]; then + create_default_conf +fi + +source "$FfmpegConf" + +rm -rf "$VideoUpscaledDir"; mkdir -p "$VideoUpscaledDir" + +# videoname? +#VideoName=$(find "$VideoDir" -type f -printf "%f\n" | head -n 1) +VideoName="0.h265" +VideoUpscaled="$VideoUpscaledDir/$VideoName" + +ffmpeg \ + -hide_banner \ + -f "image2" \ + -framerate "$(framerate)" \ + -i "$FramesUpscaledDir/%06d.png" \ + -r "$(framerate)" \ + -vcodec "$VideoCodec" \ + -preset "$Preset" \ + -pix_fmt "$PixelFormat" \ + $(auto_bitrate) \ + $(auto_x265params) \ + "$VideoUpscaled" + +if [[ $? -ne 0 ]]; then + exit "$CREATE_UPSCALED_VIDEO_ERROR" +fi diff --git a/settings/ffmpeg.conf b/settings/ffmpeg.conf new file mode 100644 index 0000000..f4b0f6d --- /dev/null +++ b/settings/ffmpeg.conf @@ -0,0 +1,6 @@ +VideoCodec="libx265" +Preset="slow" +PixelFormat="yuv420p10le" +ConstantRateFactor="16" +VideoBitrate="" +x265params="limit-sao=1:bframes=8:psy-rd=1:aq-mode=3" diff --git a/settings/video2d-2x.conf b/settings/video2d-2x.conf new file mode 100644 index 0000000..f1f3526 --- /dev/null +++ b/settings/video2d-2x.conf @@ -0,0 +1,9 @@ +### Dependencies settings ### + +# By default, the script will look for utilities (ffmpeg, waifu2x-caffe-cui, etc) in the $PATH, +# specify directories here if they are not accessible via $PATH. +# You can also specify a relative path, the directory with the "video2d-2x" script is the base directory. + +Waifu2xCaffeDir="dependencies/waifu2x-caffe" +MkvToolNixDir="dependencies/mkvtoolnix" +FfmpegDir="" diff --git a/settings/waifu2x-caffe.conf b/settings/waifu2x-caffe.conf new file mode 100644 index 0000000..3061f36 --- /dev/null +++ b/settings/waifu2x-caffe.conf @@ -0,0 +1,14 @@ +### Waifu2x-caffe settings ### + +# you can read about each parameter here: +# https://github.com/lltcggie/waifu2x-caffe/blob/master/README-EN.md#command-line-options-common + +Process="cudnn" +GpuNum="0" +ScaleRatio="3" +OutputDepth="16" +Mode="noise_scale" +CropSize="256" +BatchSize="1" +Model="upresnet10" +TtaMode="0" diff --git a/video2d-2x b/video2d-2x new file mode 100644 index 0000000..d3d4eb9 --- /dev/null +++ b/video2d-2x @@ -0,0 +1,99 @@ +#!/bin/bash + +# video2d-2x is a command line tool for upscaling videos using waifu2x. +# Copyright (C) 2020 GenZmeY +# mailto: genzmey@gmail.com +# +# This file is part of video2d-2x. +# +# video2d-2x is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +readonly ScriptName=$(basename "$0") +readonly ScriptVersion="0.1" +readonly BaseDir=$(dirname $(readlink -e "$0")) + +readonly DepsDir="$BaseDir/dependencies" +readonly ScriptsDir="$BaseDir/scripts" +readonly SettingsDir="$BaseDir/settings" +readonly OutputDir="$BaseDir/output" + +readonly FramesDir="$OutputDir/frames" +readonly AudioDir="$OutputDir/audio" +readonly VideoDir="$OutputDir/video" +readonly SubtitlesDir="$OutputDir/subtitles" +readonly ChaptersDir="$OutputDir/chapters" +readonly AttachmentsDir="$OutputDir/attachments" +readonly ReleaseDir="$OutputDir/release" + +readonly FramesUpscaledDir="${FramesDir}_upscaled" +readonly VideoUpscaledDir="${VideoDir}_upscaled" + +readonly FormatJson="$OutputDir/format.json" +readonly StreamsJson="$OutputDir/streams.json" +readonly ChaptersJson="$OutputDir/chapters.json" + +readonly RangesList="$OutputDir/ranges.list" + +readonly Video2d2xConf="$SettingsDir/video2d-2x.conf" +readonly Waifu2xConf="$SettingsDir/waifu2x-caffe.conf" +readonly FfmpegConf="$SettingsDir/ffmpeg.conf" + +source "$ScriptsDir/returncodes.lib" +source "$ScriptsDir/common.lib" + +source "$Video2d2xConf" + +function show_version () +{ + echo "$ScriptName v$ScriptVersion" +} + +function show_help () +{ + echo "$ScriptName - command line tool for upscaling videos using waifu2x" + echo " -e, --extract extract all resources from (video) FILE" + echo " -f, --frames convert extracted video to frames" + echo " -r, --ranges generate range list for frames" + echo " -u, --upscale upscale frames with range settings" + echo " -v, --video generate upscaled video from upscaled frames" + echo " -m, --merge merge upscaled video with all extracted resources" + echo " -V, --version show version" + echo " -h, --help show help" +} + +if [[ -n "$Waifu2xCaffeDir" ]]; then + PATH="$PATH:$(readlink -e $Waifu2xCaffeDir)" +fi +if [[ -n "$MkvToolNixDir" ]]; then + PATH="$PATH:$(readlink -e $MkvToolNixDir)" +fi +if [[ -n "$FfmpegDir" ]]; then + PATH="$PATH:$(readlink -e $FfmpegDir)" +fi + +if [[ $# -eq 0 ]]; then show_help; exit 0; fi +case $1 in + -h|--help ) show_help ;; + -V|--version ) show_version ;; + -e|--extract ) shift; source "$ScriptsDir/extract.sh" ;; + -f|--frames ) shift; source "$ScriptsDir/frames.sh" ;; + -r|--ranges ) shift; source "$ScriptsDir/ranges.sh" ;; + -u|--upscale ) shift; source "$ScriptsDir/upscale.sh" ;; + -v|--video ) shift; source "$ScriptsDir/video.sh" ;; + -m|--merge ) shift; source "$ScriptsDir/merge.sh" ;; + -t|--test ) shift; source "$ScriptsDir/test.sh" ;; + * ) echo "Command not recognized: $1" ; exit "$OPTION_ERROR";; +esac + +exit "$SUCCESS" \ No newline at end of file