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
ffmpegto re-encode the audio track (e.g., to AAC for better compatibility). - Copy Only – Simply extracts and moves the MKV file with no encoding.
UPLOAD and RETAIL-NEW.
⚙️ Requirements
Both scripts require the following tools to be installed:
unrar– For extracting RAR filescksfv– For verifying file integrity via .sfvffmpeg– (Only for the transcoding script)
🔧 Install on Ubuntu/Debian
sudo apt update
sudo apt install unrar cksfv ffmpeg🔁 Script 1: unrar-moviez-transcode.sh (with ffmpeg)
This script performs the following steps:
- Scans each subfolder in the
UPLOADdirectory - Checks for an
.sfvfile and verifies the archive integrity - Extracts the
.mkvfile usingunrar - Re-encodes the audio stream to AAC using
ffmpeg(video is copied without re-encoding) - Moves the result to the
RETAIL-NEWdirectory - Deletes the original folder
#!/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.
#!/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)
*/15 * * * * /home/VOD/unrar-moviez-transcode.sh >/dev/null 2>&1🔐 Notes
- Ensure that folders like
UPLOADandRETAIL-NEWexist and are writable. - Lockfile prevents multiple parallel executions (adjust path as needed).
- You can run the second script alternately if no transcoding is needed.