#!/bin/bash ROOT="${1:-.}" LOG="reencode.log" PROGRESS="reencode.progress" FAILED="reencode.failed" SAFE_DIR="${SAFE_DIR:-}" touch "$LOG" touch "$PROGRESS" ts() { date '+%F %T'; } handler() { echo "$(ts) INTERRUPTED by SIG$1 pid=$$ ppid=$PPID line=$LINENO cmd='$BASH_COMMAND'" exit 1 } trap 'handler INT' INT trap 'handler TERM' TERM count=0 find "$ROOT" -type f \( \ -iname "*.mp4" \ ! -iname "*_delete" \ \) -print0 | while IFS= read -r -d '' f; do count=$((count+1)) echo "----------------" echo "" if grep -Fxq "$f" "$PROGRESS"; then echo "$(ts) [$count] SKIP file=$f reason=already_done" continue fi echo "$(ts) [$count] START file=$f" codec=$(ffprobe -v error -select_streams v:0 \ -show_entries stream=codec_name \ -of default=noprint_wrappers=1:nokey=1 "$f") audio_codec=$(ffprobe -v error -select_streams a:0 \ -show_entries stream=codec_name \ -of default=noprint_wrappers=1:nokey=1 "$f") sub_codec=$(ffprobe -v error -select_streams s:0 \ -show_entries stream=codec_name \ -of default=noprint_wrappers=1:nokey=1 "$f") bitrate=$(ffprobe -v error -select_streams v:0 \ -show_entries stream=bit_rate \ -of default=noprint_wrappers=1:nokey=1 "$f") video_streams=$(ffprobe -v error -select_streams v \ -show_entries stream=index \ -of csv=p=0 "$f" | wc -l) echo "$(ts) [$count] INFO Codecs: v=$codec a=$audio_codec s=$sub_codec bitrate=$bitrate video_streams=$video_streams" if [[ "$codec" == "hevc" || "$codec" == "av1" ]]; then echo "$(ts) [$count] SKIP file=$f reason=codec_$codec" >> "$LOG" echo "$(ts) [$count] SKIP file=$f reason=codec_$codec" echo "$f" >> "$PROGRESS" continue fi container="mp4" case "$audio_codec" in opus|vorbis|flac) container="mkv" ;; esac case "$sub_codec" in webvtt|ass|ssa) container="mkv" ;; esac if (( video_streams > 1 )); then container="mkv" fi echo "$(ts) [$count] INFO Selected container: $container" if [[ -z "$bitrate" ]]; then bitrate=0 fi if (( bitrate > 20000000 )); then crf=25 elif (( bitrate > 10000000 )); then crf=24 elif (( bitrate > 5000000 )); then crf=23 else crf=22 fi orig_size=$(stat -c%s "$f") echo "$(ts) [$count] INFO CRF: $crf, Original Size: $orig_size" base="${f%.*}" if [[ -n "$SAFE_DIR" ]]; then rel="${f#$ROOT/}" out="$SAFE_DIR/${rel%.*}.reencode.tmp.$container" mkdir -p "$(dirname "$out")" else out="${base}.reencode.tmp.$container" fi ffmpeg -nostdin -y -v error -stats \ -i "$f" \ -profile:v high \ -level 4.1 \ -map 0:v:0 \ -map 0:a? \ -map 0:s? \ -map 0:v:2? \ -c:v:0 libx264 \ -c:v:1 copy \ -c:a copy \ -c:s copy \ -c:t copy \ -pix_fmt yuv420p \ -crf "$crf" \ -preset slow \ -map_metadata 0 \ -map_chapters 0 \ -movflags use_metadata_tags+faststart \ -disposition:v:1 attached_pic \ -vsync vfr \ -maxrate 10M \ -bufsize 20M \ "$out" ff_status=$? if [ $ff_status -ne 0 ]; then echo "$(ts) [$count] FAIL encode exit=$ff_status file=$f" >> "$LOG" echo "$(ts) [$count] FAIL encode exit=$ff_status file=$f" echo "$f" >> "$FAILED" rm -f "$out" continue fi if [[ ! -f "$out" ]]; then echo "$(ts) [$count] FAIL empty_file file=$f" >> "$LOG" echo "$(ts) [$count] FAIL empty_file file=$f" echo "$f" >> "$FAILED" continue fi duration=$(ffprobe -v error -show_entries format=duration \ -of default=noprint_wrappers=1:nokey=1 "$out") if [[ -z "$duration" || "$duration" == "0.000000" ]]; then echo "$(ts) [$count] FAIL zero_duration file=$f" >> "$LOG" echo "$(ts) [$count] FAIL zero_duration file=$f" echo "$f" >> "$FAILED" rm -f "$out" continue fi new_size=$(stat -c%s "$out") if (( new_size >= orig_size )); then rm -f "$out" echo "$(ts) [$count] SKIP larger orig=$orig_size new=$new_size bitrate=$bitrate crf=$crf file=$f" >> "$LOG" echo "$(ts) [$count] SKIP larger orig=$orig_size new=$new_size bitrate=$bitrate crf=$crf file=$f" echo "$f" >> "$PROGRESS" continue fi if [[ $? -eq 0 ]]; then touch -r "$f" "$out" if [[ "$container" == "mp4" ]]; then mv "$out" "$f" else final="${base}.mkv" mv "$out" "$final" mv "$f" "${f}_delete" fi echo "$f" >> "$PROGRESS" else echo "$(ts) [$count] ERROR encoding failed for $f" rm -f "$out" fi reduction=$(( (orig_size - new_size) * 100 / orig_size )) echo "$(ts) [$count] DONE file=$f reduction=${reduction}%" echo "$(ts) [$count] OK orig=$orig_size new=$new_size bitrate=$bitrate crf=$crf reduction=${reduction}% file=$f" >> "$LOG" echo "$f" >> "$PROGRESS" done echo "done"