Skip to main content
Home
Techies Briefcase
~ by Hari Venu

Main navigation

  • Home
  • About me
  • Photos
  • My Bookmarks

Breadcrumb

  1. Home
  2. Fixing Unplayable MP4 Videos In Jellyfin By Batch Re-encoding With FFmpeg

Fixing Unplayable MP4 Videos in Jellyfin by Batch Re-encoding with FFmpeg

By hari, 13 June, 2025

If you're using Jellyfin the open-source media server you may have come across this annoying issue: some .mp4 files just won’t play, even though they open fine in VLC or other players. This is typically due to codec incompatibilities or unusual encoding parameters that Jellyfin’s media engine can't handle well.

I recently faced this with a batch of .mp4 files. Instead of manually checking and re-encoding them one-by-one, I created a simple Bash script using ffmpeg to batch re-encode all MP4 files from a directory while preserving the original folder structure.

Below is the script and a walk through of how it works.

🔧 The Script: Re-encode MP4s for Jellyfin Compatibility

#!/bin/bash
# Input and output directories
INPUT_DIR="/home/user/video_origin"
OUTPUT_DIR="/home/user/video"
# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
echo "🔁 Starting MP4 re-encoding..."
echo "Input: $INPUT_DIR"
echo "Output: $OUTPUT_DIR"
# Find and re-encode all .mp4 files
find "$INPUT_DIR" -type f -iname "*.mp4" | while read -r FILE; do
 REL_PATH="${FILE#$INPUT_DIR/}"
 OUTPUT_FILE="$OUTPUT_DIR/$REL_PATH"
 OUTPUT_DIR_PATH=$(dirname "$OUTPUT_FILE")
 # Skip if file already exists
 if [[ -f "$OUTPUT_FILE" ]]; then
   echo "⏭️ Skipping (already exists): $REL_PATH"
   continue
 fi
 mkdir -p "$OUTPUT_DIR_PATH"
 echo "🎞️ Encoding: $REL_PATH"
 # FFmpeg encoding: video (H.264) + audio (AAC)
 ffmpeg -nostdin -y -i "$FILE" \
   -c:v libx264 -preset veryfast -crf 23 \
   -c:a aac -b:a 192k \
   "$OUTPUT_FILE"
 echo "✅ Done: $REL_PATH"
done
echo "🎉 All MP4 files have been re-encoded and saved to: $OUTPUT_DIR"

🐧 Platform Compatibility

⚠️ This script works on Linux or Unix-like systems (e.g., Ubuntu, Debian, Fedora, macOS, or Windows with WSL). It uses Bash and common tools like ffmpeg, find, and mkdir.

If you're using Windows without WSL, you can:

  • Install Git Bash and run the script there (with some path adjustments), or

  • Use a tool like HandBrake CLI for batch re-encoding.


🧠 Why This Works

  • H.264 for video (libx264) and AAC for audio are widely supported by Jellyfin’s default playback engine and most devices.

  • The script keeps original directory structures intact, which is handy if you organize by folders or genres.

  • It skips already-processed files, saving time on reruns.

  • -crf 23 with veryfast preset gives a balance between size and quality (tweakable if needed).


⚡ Prerequisites

Make sure you have:

  • ffmpeg installed (sudo apt install ffmpeg on Debian-based systems)

  • Sufficient disk space for the re-encoded copies

  • Executed this script in a safe place (so your originals remain untouched)


🚀 Running the Script

  1. Update the paths in INPUT_DIR and OUTPUT_DIR.

  2. Save the script as reencode_mp4s.sh.

  3. Make it executable:

chmod +x reencode_mp4s.sh

   4. Run it:

./reencode_mp4s.sh

✅ Result

After running the script, all your .mp4 videos will be re-encoded into a format that Jellyfin can stream smoothly—no playback errors, no surprises.


📝 Final Notes

  • If you want smaller file sizes, increase -crf (e.g., to 28); lower values improve quality but increase size.

  • You can also customize it to handle .mkv or .avi by changing the file extension match in find.

 

 

 

Tags

  • Linux
  • Jellyfin
  • Bash Script
 

Topics

  • Composer
  • Commad Line Tools
  • JavaScript
  • Drupal
  • Solar Panel
  • Permission
  • Libraries
  • MySQL
  • Commad Line
  • RSA
  • VSCode
  • Windows
  • Entity Query API
  • Ubuntu
  • Bash Script
  • Drush
  • Bower
  • Errors
  • Workflow
  • File API
  • Docker
  • Linux
  • Lando
  • Articles
  • Views
  • Nginx
  • Twig
  • Multilingual
  • OOPs
  • Coder
  • Solar Power
  • PHP
  • Field API
  • Android
  • Cache
  • Webform
  • jQuery
  • phpcbf
  • Ajax API
  • Distribution
  • Drupal Behavior
  • Learning
  • kali
  • professional development
  • Neuron AI
  • Jellyfin
  • SSH
  • URL
  • Macbook
  • WSL2
  • Drupal AI
  • NPM
  • Git
  • Form API
  • phpcs
  • Drupal Console
  • Codesniffer
  • Moderation
  • PHPSeclib
  • Taxonomy
  • Mac OS
RSS Feed

Who am I !!!

Hari Venu V
harivenu.v1992@gmail.com
Mobile : +91 9895164181
Whatsapp : +91 9895164181
Linkedin
Drupal.org
Github

Document
Resume.pdf (114.38 KB)

Work !!!

Fulltime Developer @ QED42
hari.venu@qed42.com
Linkedin

Powered by Drupal & Maintained by Hari