🎬 Unrar Moviez Script Suite

Two Bash scripts that extract movie releases from RAR archives with SFV check and either copy the MKV or re-encode the audio with ffmpeg.

Category
🎬 Media
Requirements
See section below
Code blocks
4

This guide documents two bash scripts designed to automate the processing of movie releases (e.g., from Usenet or scene releases) that are stored in .rar archives and optionally include .sfv verification files.

Choose between two modes:

  • With Transcoding – Uses ffmpeg to re-encode the audio track (e.g., to AAC for better compatibility).
  • Copy Only – Simply extracts and moves the MKV file with no encoding.
Recommended for: Automated server environments, Plex/Emby setups, or NAS workflows with folders like UPLOAD and RETAIL-NEW.

⚙️ Requirements

Both scripts require the following tools to be installed:

  • unrar – For extracting RAR files
  • cksfv – For verifying file integrity via .sfv
  • ffmpeg – (Only for the transcoding script)

🔧 Install on Ubuntu/Debian

Bash
sudo apt update
sudo apt install unrar cksfv ffmpeg

🔁 Script 1: unrar-moviez-transcode.sh (with ffmpeg)

This script performs the following steps:

  1. Scans each subfolder in the UPLOAD directory
  2. Checks for an .sfv file and verifies the archive integrity
  3. Extracts the .mkv file using unrar
  4. Re-encodes the audio stream to AAC using ffmpeg (video is copied without re-encoding)
  5. Moves the result to the RETAIL-NEW directory
  6. Deletes the original folder
Bash
#!/bin/bash
set -uo pipefail
umask 077
shopt -s nullglob

# === Configuration ===
bindir="/home/VOD"
lockfile="$bindir/.lockfile_unrar"
upload="$bindir/UPLOAD"
files="$bindir/RETAIL-NEW"

# === Dependency check ===
for cmd in cksfv unrar ffmpeg flock; do
  if ! command -v "$cmd" &>/dev/null; then
    echo "Error: $cmd is not installed."
    exit 1
  fi
done

# === Prevent parallel execution ===
exec 9>"$lockfile" || exit 1
flock -n 9 || { echo "Script already running."; exit 1; }
[[ -d "$files" && -w "$files" ]] || { echo "Output directory unavailable."; exit 1; }
failed=0

for dir in "$upload"/*/; do
  [ -d "$dir" ] || continue
  echo "[*] Processing $dir"
  cd "$dir" || { failed=1; continue; }

  if ls *.sfv 1> /dev/null 2>&1; then
    echo "Verifying archive..."
    cksfv -q *.sfv || { failed=1; continue; }
  fi

  archives=( *.rar )
  if (( ${#archives[@]} == 0 )) || ! unrar e -y "${archives[0]}"; then
    failed=1; continue
  fi
  videos=( *.mkv )
  if (( ${#videos[@]} != 1 )); then
    echo "Expected exactly one MKV; preserving source: $dir" >&2
    failed=1; continue
  fi
  mkv=${videos[0]}

  if [ -n "$mkv" ]; then
    out="$files/$(basename "$dir").mkv"
    echo "Transcoding audio..."
    if [[ -e "$out" ]] || ! ffmpeg -n -i "$mkv" -c:v copy -c:a aac -b:a 160k "$out" || [[ ! -s "$out" ]]; then
      echo "Transcoding failed; source preserved: $dir" >&2
      failed=1; continue
    fi
    echo "[✓] Done: $out"
  fi

  cd "$bindir" || exit 1
  rm -rf -- "$dir" || failed=1
done

# Keep the lock inode; the descriptor is released automatically.
exit "$failed"

📦 Script 2: unrar-moviez-copy.sh (copy only)

Same as above, but without transcoding. It simply extracts and moves the first .mkv file found.

Bash
#!/bin/bash
set -uo pipefail
umask 077
shopt -s nullglob

# === Configuration ===
bindir="/home/VOD"
lockfile="$bindir/.lockfile_unrar"
upload="$bindir/UPLOAD"
files="$bindir/RETAIL-NEW"

# === Dependency check ===
for cmd in cksfv unrar flock; do
  if ! command -v "$cmd" &>/dev/null; then
    echo "Error: $cmd is not installed."
    exit 1
  fi
done

# === Prevent parallel execution ===
exec 9>"$lockfile" || exit 1
flock -n 9 || { echo "Script already running."; exit 1; }
[[ -d "$files" && -w "$files" ]] || { echo "Output directory unavailable."; exit 1; }
failed=0

for dir in "$upload"/*/; do
  [ -d "$dir" ] || continue
  echo "[*] Processing $dir"
  cd "$dir" || { failed=1; continue; }

  if ls *.sfv 1> /dev/null 2>&1; then
    echo "Verifying archive..."
    cksfv -q *.sfv || { failed=1; continue; }
  fi

  archives=( *.rar )
  if (( ${#archives[@]} == 0 )) || ! unrar e -y "${archives[0]}"; then
    failed=1; continue
  fi
  videos=( *.mkv )
  if (( ${#videos[@]} != 1 )); then
    echo "Expected exactly one MKV; preserving source: $dir" >&2
    failed=1; continue
  fi
  mkv=${videos[0]}

  if [ -n "$mkv" ]; then
    out="$files/$(basename "$dir").mkv"
    if [[ -e "$out" ]] || ! mv -- "$mkv" "$out" || [[ ! -s "$out" ]]; then
      echo "Move failed; source preserved: $dir" >&2
      failed=1; continue
    fi
    echo "[✓] Copied: $files/$(basename "$dir").mkv"
  fi

  cd "$bindir" || exit 1
  rm -rf -- "$dir" || failed=1
done

# Keep the lock inode; the descriptor is released automatically.
exit "$failed"

📅 Automation (Optional)

Cron
*/15 * * * * /home/VOD/unrar-moviez-transcode.sh >/dev/null 2>&1

🔐 Notes

  • Ensure that folders like UPLOAD and RETAIL-NEW exist and are writable.
  • Lockfile prevents multiple parallel executions (adjust path as needed).
  • You can run the second script alternately if no transcoding is needed.

← Back to all scripts