-
-
Save zoff99/31281bb40ceb3c78485da108de815ad4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
threads=5 | |
quiet=1 | |
function sanitize_file_name { | |
echo -n "$1" | perl -pe 's/[\?\[\]\/\\=<>:'"'"';,"&\$#*()|~`!{}%+]//g;' -pe 's/[\r\n\t -]+/_/g;' | |
} | |
stty -echoctl # hide ^C | |
ffmpeg_path="/usr/local/ffmpeg/bin/" | |
printf '%q\n' * | while IFS= read filename ; do | |
if [ -f "$filename" ]; then | |
if [[ "$filename" =~ .*"__2.hw.mkv" ]]; then | |
echo "INF: already converted file, ignoring: (${filename})" | |
else | |
filename_sanitized=$(sanitize_file_name "$filename") | |
if [ "$filename" != "$filename_sanitized" ]; then | |
echo 'INF: sanitizing filename to '"$filename_sanitized" | |
mv "$filename" "$filename_sanitized" 2>/dev/null | |
fi | |
# get duration in seconds | |
video_duration_secs=$("$ffmpeg_path"/ffprobe './'"$filename_sanitized" -show_entries format=duration -v quiet -of csv="p=0" 2> /dev/null) | |
# check for strange fps | |
"$ffmpeg_path"/ffprobe './'"$filename_sanitized" 2>&1|grep '59.95 fps' > /dev/null | |
res=$? | |
echo "INF: converting ${filename_sanitized} ..." | |
if [ $res -eq 0 ]; then | |
: | |
if [ "$quiet""x" == "1x" ]; then | |
nice "$ffmpeg_path"/ffmpeg -nostdin \ | |
-y -threads "$threads" -i './'"$filename_sanitized" \ | |
-vcodec h264_nvenc -coder cabac -preset:v p1 -cq 36 -rc vbr \ | |
-filter:v fps=fps=30 \ | |
-c:a aac -sn './'"$filename_sanitized"'__2.hw.mkv' > /dev/null 2>&1 | |
else | |
nice "$ffmpeg_path"/ffmpeg -nostdin \ | |
-y -threads "$threads" -i './'"$filename_sanitized" \ | |
-vcodec h264_nvenc -coder cabac -preset:v p1 -cq 36 -rc vbr \ | |
-filter:v fps=fps=30 \ | |
-c:a aac -sn './'"$filename_sanitized"'__2.hw.mkv' | |
fi | |
else | |
: | |
if [ "$quiet""x" == "1x" ]; then | |
nice "$ffmpeg_path"/ffmpeg -nostdin \ | |
-y -threads "$threads" -i './'"$filename_sanitized" \ | |
-vcodec h264_nvenc -coder cabac -preset:v p1 -cq 36 -rc vbr \ | |
-c:a aac -sn './'"$filename_sanitized"'__2.hw.mkv' > /dev/null 2>&1 | |
else | |
nice "$ffmpeg_path"/ffmpeg -nostdin \ | |
-y -threads "$threads" -i './'"$filename_sanitized" \ | |
-vcodec h264_nvenc -coder cabac -preset:v p1 -cq 36 -rc vbr \ | |
-c:a aac -sn './'"$filename_sanitized"'__2.hw.mkv' | |
fi | |
fi | |
# keep timestamp of the original file | |
touch -r './'"$filename_sanitized" './'"$filename_sanitized"'__2.hw.mkv' >/dev/null 2>/dev/null | |
# get duration in seconds of converted file | |
video_duration_secs_conv=$("$ffmpeg_path"/ffprobe './'"$filename_sanitized"'__2.hw.mkv' -show_entries format=duration -v quiet -of csv="p=0" 2> /dev/null) | |
echo "INF: secs orig: ${video_duration_secs} -> secs conv: ${video_duration_secs_conv}" | |
if [ "$video_duration_secs""x" = "x" ]; then | |
rm -f './'"$filename_sanitized"'__2.hw.mkv' >/dev/null 2>/dev/null | |
echo "ERR: orig video duration unknown (${filename_sanitized})" | |
elif [ "$video_duration_secs""x" = "0x" ]; then | |
rm -f './'"$filename_sanitized"'__2.hw.mkv' >/dev/null 2>/dev/null | |
echo "ERR: orig video duration zero (${filename_sanitized})" | |
else | |
duration_ok=$(echo "($video_duration_secs / 1) <= ($video_duration_secs_conv / 1)" 2>/dev/null|bc 2>/dev/null) | |
if [ "$duration_ok""x" == "1x" ]; then | |
# size_orig=$(stat -c '%s' "$filename") | |
size_conv=$(stat -c '%s' "$filename_sanitized") | |
echo "INF: filesize : ${size_conv} (${filename_sanitized})" | |
err1=$(echo "scale=3; $size_conv > 2000000.0" 2>/dev/null| bc 2>/dev/null) | |
if [ "$err1""x" == "x" ]; then | |
rm -f './'"$filename_sanitized"'__2.hw.mkv' >/dev/null 2>/dev/null | |
echo "ERR: --- (${filename_sanitized})" | |
elif [ "$err1""x" == "1x" ]; then | |
rm -f "$filename_sanitized" >/dev/null 2>/dev/null | |
echo "OK : deleted orig file (${filename_sanitized})" | |
else | |
rm -f './'"$filename_sanitized"'__2.hw.mkv' >/dev/null 2>/dev/null | |
echo "ERR: file smaller than 2MByte (${filename_sanitized})" | |
fi | |
else | |
rm -f './'"$filename_sanitized"'__2.hw.mkv' >/dev/null 2>/dev/null | |
echo "ERR: converted video length too short (${filename_sanitized})" | |
fi | |
fi | |
fi | |
fi | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment