Jump to content

Fixed - choppy/ stuttering playback on some HEVC RARBG files


Recommended Posts

Machete89
Posted

Hi everyone, long time premiere user here, first time posting on these forums. I apologize in advance if this is the wrong section to post this. I have searched and haven't really found a detailed solution like this posted.

I'm running on Linux mint 22.1 with Emby server 4.8.11.0

I have a solution to a problem that had been haunting me for a while now and maybe some users can benefit from this information.

My main issue I was experiencing was very choppy/ stuttery play back on some HEVC encoded movies. Specifically the movies encoded by RARBG in 1080p x265 with the .mp4 file extension. I didn't experience this issue in the older server versions before version 4.8.11.0 because I clearly remember playing these movies on earlier server versions without issues. This issue was present when using the 1st and 2nd generation of the Amazon Fire Stick 4k Max on multiple tv's using Emby for Android 3.4.74. I don't know if it's something about these specific movies from RARBG that's not playing well with the newest version of the app or what, because they all play fine in VLC on my desktop. All movies were playing in Direct Play mode when I experienced the choppy playback, and transcoding fixed the issue temporarily, but was not a permanent fix good enough for me. Oddly enough, these movies played back fine on my Android phone using the same app version 3.4.74, but did not playback well on my fire sticks, I can't explain why. I did try out Plex, but still had the same playback issue. 

Here's the permanent solution that worked for me: I discovered that by remuxing the affected .mp4 files in a program called MKVToolNix solved all my playback issues. This program re-packages the raw video and audio into an MKV container. It also creates a fresh, clean indexing information (important for playback). You can also include any external subtitles all packaged into the new .mkv file. After solving the playback of 1 movie, my next issue was repeating the same process for the rest of my big library of encoded RARBG movies. I used AI to help me write a simple script that uses the command line version of MKVToolNix that searches for any .mp4 file in a folder and subfolders, and remuxes every .mp4 file it finds (and includes external subtitles). After the successful remux and creation of the new mkv movie file , the script deletes the input .mp4 to save space (never do this without a backup just in case). This whole process took my desktop over 90 hours to complete a library of over 1400 affected files, but it was successful and I can now watch every single movie without playback issues. If you have internal hard drives or ssd's I'm sure this process will be much quicker. 

I hope this helps someone that is experiencing playback issues. If you have any questions about my solution let me know.

I want to say thank you to the Emby team for all the hard work you do, this is a fantastic app that I use daily! 

  • Thanks 2
Posted

Hi, thanks for sharing !

  • 6 months later...
spongmiester
Posted

You don't still have the script lying around do you? I've got 7000 of their movies to go through 😭.

  • 3 weeks later...
Machete89
Posted

Hey, sorry for the late reply, I completely forgot to get back to you..

Here's the code I use all the time now if anyone else is interested as well. This will ask you if you want to delete the original mp4 files after conversion or not, and a log file will be created after it's done. It will search through the directory including all subfolders. It will also merge the mp4 files with a subtitle if it has the same file name in the same folder. 

I'm on Linux Mint 22.1 and using MKVtoolnix command-line version 82.0-1build2

I hope this helps.

#!/bin/bash

# Define the log file path and name
LOG_FILE="./mkv_conversion_$(date +%Y%m%d_%H%M%S).log"

# --- Function to log messages ---
log_message() {
    echo "$1" | tee -a "$LOG_FILE"
}

# --- Initial setup for logging ---
echo "--- MKV Batch Conversion Log: $(date) ---" > "$LOG_FILE"
log_message "Starting MP4 to MKV batch conversion..."
log_message "Log file: $LOG_FILE"
log_message "----------------------------------------"

# Feature 1: Prompt for deleting original MP4 files
DELETE_ORIGINAL="n" # Default to no deletion
read -p "Do you want to delete the original MP4 files after successful conversion? (y/N): " user_choice
user_choice=${user_choice,,} # Convert to lowercase
if [[ "$user_choice" == "y" || "$user_choice" == "yes" ]]; then
    DELETE_ORIGINAL="y"
    log_message "Original MP4 files will be deleted after successful conversion."
else
    log_message "Original MP4 files will be kept."
fi
log_message "---"

# --- New Feature: File Counter ---
# Find all .mp4 files and store their paths in an array
mapfile -d $'\0' ALL_MP4_FILES < <(find . -type f -name "*.mp4" -print0)
TOTAL_FILES=${#ALL_MP4_FILES[@]}
FILES_PROCESSED=0

log_message "Found $TOTAL_FILES MP4 files to process."
echo "Found $TOTAL_FILES MP4 files to process." # Also print to terminal for immediate feedback
echo "---"

# Now loop through the array of files
for f_indexed in "${ALL_MP4_FILES[@]}"; do
    # Remove null terminator from the end of each filename if present (from mapfile -d $'\0')
    f="${f_indexed%$'\0'}" 

    # Skip if f is empty, which can happen if mapfile has trailing null byte
    if [ -z "$f" ]; then
        continue
    fi

    FILES_PROCESSED=$((FILES_PROCESSED + 1))
    FILES_LEFT=$((TOTAL_FILES - FILES_PROCESSED))

    echo "Processing file $FILES_PROCESSED of $TOTAL_FILES ($FILES_LEFT remaining):" | tee -a "$LOG_FILE"
    log_message "Processing: '$f'"

    # Extract filename without extension for the output name
    filename_no_ext="${f%.mp4}"
    
    # Extract directory path
    dir_name=$(dirname "$f")

    # Construct the output MKV filename in the same directory as the original MP4
    output_mkv="${filename_no_ext}.mkv"

    # Initialize mkvmerge arguments
    mkvmerge_args=("-o" "$output_mkv")

    # Always add the video file as an input
    mkvmerge_args+=("$f")

    # Feature 2: Detect and add subtitle files
    subtitle_extensions=("srt" "ass" "ssa" "vtt" "sub" "idx")
    base_video_name=$(basename "$filename_no_ext")

    found_subtitle_files=() 
    for ext in "${subtitle_extensions[@]}"; do
        subtitle_file="${dir_name}/${base_video_name}.${ext}"
        if [ -f "$subtitle_file" ]; then
            log_message "  - Found subtitle file: '$subtitle_file'"
            mkvmerge_args+=("--language" "0:eng" "$subtitle_file")
            found_subtitle_files+=("$subtitle_file")
        fi
    done

    # Display real-time progress on terminal, log verbose output
    echo "  - Remuxing '$f'..."
    log_message "  - Running mkvmerge command: mkvmerge ${mkvmerge_args[*]}"
    
    # Create a temporary file for mkvmerge's stderr output
    TEMP_ERR_FILE=$(mktemp)

    if ! mkvmerge "${mkvmerge_args[@]}" > >(tee -a "$LOG_FILE") 2> "$TEMP_ERR_FILE"; then
        log_message "--- mkvmerge ERROR DETAILS (from stderr) for '$f' ---"
        cat "$TEMP_ERR_FILE" | tee -a "$LOG_FILE" # Also show error on terminal
        log_message "--- End of mkvmerge ERROR DETAILS ---"
        CONVERSION_SUCCESS=1
    else
        CONVERSION_SUCCESS=0
    fi

    cat "$TEMP_ERR_FILE" >&2 # This prints the stderr to terminal (for progress)
    cat "$TEMP_ERR_FILE" >> "$LOG_FILE" # Also append to log
    rm "$TEMP_ERR_FILE" # Clean up temp file

    if [ "$CONVERSION_SUCCESS" -eq 0 ]; then
        log_message "Successfully converted '$f' to '$output_mkv'."
        if [ "$DELETE_ORIGINAL" == "y" ]; then
            rm "$f"
            log_message "Original '$f' deleted."
        else
            log_message "Original '$f' kept as per user choice."
        fi
        
        if [ "$DELETE_ORIGINAL" == "y" ] && [ ${#found_subtitle_files[@]} -gt 0 ]; then
            for sub_path in "${found_subtitle_files[@]}"; do
                rm "$sub_path"
                log_message "Associated subtitle '$sub_path' deleted."
            done
        fi

    else
        log_message "!!! ERROR: mkvmerge failed for '$f'. Original file NOT deleted. Please investigate. Check log for details."
    fi
    log_message "---"
done

log_message "Batch conversion complete."
log_message "----------------------------------------"
log_message "End of log: $(date)"

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...