VideoHelp Forum




+ Reply to Thread
Results 1 to 2 of 2
  1. I have tired to create a python script to identify and remove forced subs. The script identifies the correct sub ids to remove but fails to remove them. Here is the script, anybody have any ideas?

    import subprocess
    import os
    import re

    # Get the directory of the currently running script
    script_dir = os.path.dirname(os.path.realpath(__file__))

    # Function to check for forced subtitles and return their track IDs
    def get_forced_subtitle_track_ids(mkv_file):
    """Get a list of forced subtitle track IDs from the MKV file."""
    result = subprocess.run(['mkvinfo', mkv_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output = result.stdout.decode('utf-8')

    # Enhanced regex to find all forced subtitle tracks by matching the "Forced display" flag
    forced_subtitle_tracks = re.findall(r'\+ Track.*?Forced display.*?track ID for mkvmerge & mkvextract: (\d+)', output, re.DOTALL)

    # Check for a possible single line description of forced subtitles
    if not forced_subtitle_tracks:
    forced_subtitle_tracks = re.findall(r'Forced display: Yes.*?track ID for mkvmerge & mkvextract: (\d+)', output, re.DOTALL)

    # Convert the track IDs to integers and subtract 1
    adjusted_subtitle_tracks = [str(int(track_id) - 1) for track_id in forced_subtitle_tracks]

    return adjusted_subtitle_tracks

    # Function to exclude forced subtitles in the MKV file
    def exclude_forced_subtitles(mkv_file):
    """Remove forced subtitle tracks from the MKV file."""
    # Get the list of forced subtitle track IDs
    forced_subtitles = get_forced_subtitle_track_ids(mkv_file)

    # If there are no forced subtitles, skip processing
    if not forced_subtitles:
    print(f"No forced subtitles found in {mkv_file}. Skipping...")
    return

    # Create a comma-separated string of forced subtitle track IDs
    forced_subtitles_str = ','.join(forced_subtitles)

    # Build the mkvmerge command to exclude the forced subtitle tracks using the -s !
    mkvmerge_command = ['mkvmerge', '-o', f'no_forced_{os.path.basename(mkv_file)}', mkv_file, '-s', f'!{forced_subtitles_str}']

    # Debugging: Print the command being executed
    print(f"Running command: {' '.join(mkvmerge_command)}")

    # Run the mkvmerge command and check for errors
    result = subprocess.run(mkvmerge_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    # Print the result of the mkvmerge command for debugging
    if result.returncode != 0:
    print(f"Error excluding forced subtitles from {mkv_file}: {result.stderr.decode('utf-8')}")
    else:
    print(f"Forced subtitles excluded and saved as 'no_forced_{os.path.basename(mkv_file)}'")

    # Process all MKV files in the current directory
    for file_name in os.listdir(script_dir):
    if file_name.endswith(".mkv"):
    mkv_file = os.path.join(script_dir, file_name)

    print(f"Processing {mkv_file}...")

    # Exclude forced subtitles from the file
    exclude_forced_subtitles(mkv_file)
    Quote Quote  
  2. Python indentation disappears if printed outside of code tags, put your text into a CODE tags.
    Example: [ CODE ]your script here[ /CODE ] , no gaps in those square brackets,
    so just re-posting it, not correcting it:
    Code:
    import subprocess
    import os
    import re
    
    # Get the directory of the currently running script
    script_dir = os.path.dirname(os.path.realpath(__file__))
    
    # Function to check for forced subtitles and return their track IDs
    def get_forced_subtitle_track_ids(mkv_file):
        """Get a list of forced subtitle track IDs from the MKV file."""
        result = subprocess.run(['mkvinfo', mkv_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = result.stdout.decode('utf-8')
    
        # Enhanced regex to find all forced subtitle tracks by matching the "Forced display" flag
        forced_subtitle_tracks = re.findall(r'\+ Track.*?Forced display.*?track ID for mkvmerge & mkvextract: (\d+)', output, re.DOTALL)
    
        # Check for a possible single line description of forced subtitles
        if not forced_subtitle_tracks:
            forced_subtitle_tracks = re.findall(r'Forced display: Yes.*?track ID for mkvmerge & mkvextract: (\d+)', output, re.DOTALL)
    
        # Convert the track IDs to integers and subtract 1
        adjusted_subtitle_tracks = [str(int(track_id) - 1) for track_id in forced_subtitle_tracks]
    
        return adjusted_subtitle_tracks
    
    # Function to exclude forced subtitles in the MKV file
    def exclude_forced_subtitles(mkv_file):
        """Remove forced subtitle tracks from the MKV file."""
        # Get the list of forced subtitle track IDs
        forced_subtitles = get_forced_subtitle_track_ids(mkv_file)
    
        # If there are no forced subtitles, skip processing
        if not forced_subtitles:
            print(f"No forced subtitles found in {mkv_file}. Skipping...")
            return
    
        # Create a comma-separated string of forced subtitle track IDs
        forced_subtitles_str = ','.join(forced_subtitles)
    
        # Build the mkvmerge command to exclude the forced subtitle tracks using the -s !
        mkvmerge_command = ['mkvmerge', '-o', f'no_forced_{os.path.basename(mkv_file)}', mkv_file, '-s', f'!{forced_subtitles_str}']
    
        # Debugging: Print the command being executed
        print(f"Running command: {' '.join(mkvmerge_command)}")
    
        # Run the mkvmerge command and check for errors
        result = subprocess.run(mkvmerge_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
        # Print the result of the mkvmerge command for debugging
        if result.returncode != 0:
            print(f"Error excluding forced subtitles from {mkv_file}: {result.stderr.decode('utf-8')}")
        else:
            print(f"Forced subtitles excluded and saved as 'no_forced_{os.path.basename(mkv_file)}'")
    
    
    # Process all MKV files in the current directory
    for file_name in os.listdir(script_dir):
        if file_name.endswith(".mkv"):
            mkv_file = os.path.join(script_dir, file_name)
    
            print(f"Processing {mkv_file}...")
            
            # Exclude forced subtitles from the file
            exclude_forced_subtitles(mkv_file)
    Last edited by _Al_; 3rd Jan 2025 at 13:50.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!