VideoHelp Forum




+ Reply to Thread
Results 1 to 3 of 3
  1. Hello,

    Can someone please help me with building the batch script?

    I'm trying to create a batch process for my video files, i have mkv files with embedded subtitle and i want to extract a specific language subtitle to srt file and remove all subtitles within that mkv file, i also want to preserve the creation/modified date of the file.

    Here are the steps for my need:
    - Search within a folder with sub-folders all the mkv files
    - Check every mkv file for subtitles
    - If subtitles exists, search for Hebrew Subtitles
    - If Hebrew subtitles exists, than extract it to .srt file
    - When the subtitle been extract, mux the mkv file to remove all embedded subtitles from that mkv file
    - Remove old file
    - Rename new file with the original name
    - Preserve creation/modified date of the file

    i've searched the forum and found a script that i can use, so i tried to modify it for my purpose but unfortunately i'm getting a lot of errors and i don't have the knowledge to fix it.

    this is my modified script:
    Code:
    REM @ECHO OFF
    REM // =======================
    REM || Batch Subtitle Muxing
    REM ||       for MKVmerge
    REM || v0.4-20150511 by drudge
    REM \\ =======================
    REM @@ This batch script will scan the provided directory (or current folder)
    REM @@ for video files, attempt to match them with subtitle files,
    REM @@ and mux them using the MKVmerge command line.
    REM     // ---- ---- ---- ----
    REM || USER CONFIGURATION
    REM // full path to mkvmerge.exe
    REM     SET muxpath=C:\Program Files\MKVToolNix\mkvmerge.exe
    REM // full path to mkvmerge.exe
    REM     SET extractpath=C:\Program Files\MKVToolNix\mkvextract.exe
    REM \\ -- -- -- -- --
    REM // full path to unrar program, make sure to include switches
    REM     SET rarpath=C:\Program Files\7-Zip\7z.exe
    REM     SET rarcmd=e
    REM \\ -- -- -- -- --
    REM // output path (with trailing slash)
    REM ++ leave blank for working directory
            SET outputdir=
    REM @@ default: SET "outputdir="
    REM \\ -- -- -- -- --
    REM // prepend text to output filename
    REM ++ REQUIRED if outputdir is left blank
            SET filesuffix=-new
    REM @@ blank: SET "filesuffix="
    REM \\ -- -- -- -- --
    REM || END USER CONFIG
    REM \\ ---- ---- ---- ----
    REM -- editing below this line should be done precisely. (here thar be dragons)
    REM ===========================================================================
     
    REM @@ simple counters
    SET /A "mc=0"
    SET /A "me=0"
     
    REM @@ default working path
    SET wp=
     
    REM @@ optional working path via argument
    IF EXIST "%~1" SET wp=%~1
     
    REM @@ ready steady go!
    CLS
    ECHO ===========================================================================
     
    REM @@ attempt to use custom setting for output path
    IF EXIST "%outputdir%" (
            REM @@ user has provided output path
            ECHO == User Setting -- Output to: [%outputdir%]
    ) ELSE (
            REM @@ no custom setting, check for suffix
            IF [%filesuffix%]==[] (
                    ECHO @@ ERROR: empty [filesuffix] setting requires [outputdir] to be set
                    SET /A me+=1
                    GOTO:done
            )
            REM @@ use working path for output
            SET outputdir=%wp%
    )
    FOR /r %%H IN ("%wp%") DO (
            REM @@ because "." doesn't tell us where we are
            ECHO == Scanning [%%~dpfH\] for video files...
    )
     
    :GETFILES
    FOR /r %%I IN ("%wp%*.mkv") DO (
            REM @@ found a video file, now check for subtitles
            CALL:SUBSEARCH "%%~I"
    )
    GOTO:done
     
    :SUBSEARCH
    FOR /f %%A in (' mkvmerge -i "%~dpnx1" ^| FIND /c /i "subtitles" ') DO (
        IF %%A==0 (
                ECHO @@ ERROR: "%~nx1" has no subtitles
                GOTO:eof
        )
        ELSE (
            ECHO "%~nx1" has subtitles, searching for Hebrew subtitles.
            FOR %%B in (' mkvmerge -F verbose-text -i "%~dpnx1" ^| FINDSTR /i /r /c:"subtitles.*heb" ') DO (
                IF %ERRORLEVEL% EQU 0 (
                        SET substring=mkvmerge -F verbose-text -i "%~dpnx1" ^| FINDSTR /i /r /c:"subtitles.*heb"
                        CALL SET track=%substring:~9,1%
                        ECHO HEBREW Track ID: %track%
                        CALL SET line= %track%:"%~n1.heb.forced.srt"
                        CALL :SUBEXTRACT
                 ) ELSE (
                        ECHO %~nx1 do not have Hebrew subtitles.
                        CALL :MUXIT
                        )
                )
            )
    )
        
        
    :SUBEXTRACT
    setlocal enabledelayedexpansion
    mkvextract --ui-language en tracks "%~dpn1.mkv" !line! ||(ECHO Demuxing error!&GOTO:eof)
    endlocal
    CALL:MUXIT
    
     
    :MUXIT
    REM @@ we've made it!
    SET /A mc+=1
    REM @@ now we let mkvmerge work its magic
    ECHO | SET /p muxdone="++ Muxing: (%mc%) [%~n1%~x1]"
    mkvmerge -o "%outputdir%%~n1%filesuffix%%~x1" --no-subtitles --language 0:eng --default-track 0:yes --language 1:eng --default-track 1:yes "%~1" --track-order 0:0,0:1
    ECHO  ..complete
    REM @@ success!
    REM @@ remove old file
    ECHO removing old file...
    IF EXIST "%outputdir%%~n1%filesuffix%%~x1" if NOT ERRORLEVEL 1 (
              DEL "%~f1"
              REN "%outputdir%%~n1%filesuffix%%~x1" "%%~nx"
    GOTO:eof
    
    :done
    ECHO == Finished Processing: %mc% completed / %me% errors
    ECHO ===========================================================================
    REM @@ game over, man
    pause
    This is my folders structure:

    Movies
    ├─Movie_abc [...]
    │ ├─Movie_abc.mkv

    ├─Movie_def [...]
    │ ├─Movie_def.mkv

    ├─Movie_ghi [...]
    │ ├─Movie_ghi.mkv



    TV Shows
    ├─Series _abc [...]
    │ ├─Season 1
    │ ├─ Episode_abc.mkv
    │ ├─ Episode_def.mkv

    ├─Series _def [...]
    │ ├─Season 1
    │ ├─ Episode_abc.mkv
    │ ├─ Episode_def.mkv

    ├─Series _ghi [...]
    │ ├─Season 1
    │ ├─ Episode_abc.mkv
    │ ├─ Episode_def.mkv



    Thank in advanced for anyone that can help me.
    Last edited by BuSHari; 12th Oct 2017 at 12:19.
    Quote Quote  
  2. I always get the error:
    Code:
    'ELSE' is not recognized as an internal or external command,
    operable program or batch file.
    i can't figure what is the problem with the ELSE
    Quote Quote  
  3. Thanks for all the help...

    But i`ve succeeded to build my own bash script, so if anyone need it (run it on linux).

    Code:
    #!/bin/bash
    ## 2017-10-20 By Idan Bush ##
    
    # Check if tools are exists
    hash mkvmerge 2>/dev/null || { echo >&2 "Error: Command mkvmerge not found"; exit 1; }
    hash mkvextract 2>/dev/null || { echo >&2 "Error: Command mkvextract not found"; exit 1; }
    
    
    extract_subtitles_from_file() {
            local mkvfilename="$1"
            local subline="$2"
            filename=${mkvfilename##*/}
    
            # Get track id of subtitle
            tracknumber=`echo $subline | egrep -o "[0-9]{1,2}" | head -1`
            # Define the subtitle filename format
            subfilename=${mkvfilename%.*}".heb.forced.srt"
            subname=${subfilename##*/}
    
            if [ -f "$subfilename"  ];
            then
                echo "[ SKIP  ] '$subname' already exists."
            else
                echo "[EXTRACT] '$filename' -> '$subname'."
                mkvextract tracks "$mkvfilename" $tracknumber:"$subfilename"
                `chmod a+rwx "$subfilename"`
            fi
    }
    
    remove_subtitles_from_file() {
            local mkvfilename="$1"
            # Extracting filename
            filename=${mkvfilename##*/}
    
            # Get base name for new file
            newmkvfilename=${mkvfilename%.*}"-new.mkv"
            # Extracting filename
            newfilename=${newmkvfilename##*/}
    
            # Remving all subtitles
            echo "[ REMUX ] Removing all subtitles from '$filename'."
            mkvmerge -o "$newmkvfilename" --no-subtitles --language 0:eng --default-track 0:yes --language 1:eng --default-track 1:yes "$mkvfilename" --track-order 0:0,0:1
    
            # Copy modified date from original file
            echo "[ COPY DATE ] Modified date copied."
            touch -d "$(date -R -r "$mkvfilename")" "$newmkvfilename"
    
            # Delete original file
            echo "[ DELETE ] Deleting the file '$filename'."
            rm "$mkvfilename"
    
            # Rename new file with original file
            echo "[ RENAME ] '$newfilename' > '$filename'."
            mv "$newmkvfilename" "$mkvfilename"
            `chmod a+rwx "$mkvfilename"`
    }
    
    rename_subtitles() {
            local subfilename="$1"
            # Extracting filename
            filename=${subfilename##*/}
    
            if [[ "$subfilename" != *".heb.forced.srt" ]] && [[ "$subfilename" != *".heb.srt" ]] && [[ "$subfilename" != *".he.forced.srt" ]] && [[ "$subfilename" != *".he.srt" ]]; then
                
                # Removing extension
                subname=${subfilename%.*}
                # New subtitle name
                newsubname="$subname.heb.forced.srt"
                # Extracting filename
                newfilename=${newsubname##*/}
                
                # Renaming
                echo "[ RENAME ] '$subfilename' > '$newsubname'."
                mv "$subfilename" "$newsubname"
                `chmod a+rwx "$newsubname"`
    
            elif [[ "$subfilename" == *".heb.srt" ]] || [[ "$subfilename" == *".he.srt" ]] || [[ "$subfilename" == *".he.forced.srt" ]]; then
                
                # Removing extension
                subname=${subfilename%%.he*}
                # New subtitle name
                newsubname="$subname.heb.forced.srt"
                # Extracting filename
                newfilename=${newsubname##*/}
    
                # Renaming
                echo "[ RENAME ] '$subfilename' > '$newsubname'."
                mv "$subfilename" "$newsubname"
                `chmod a+rwx "$newsubname"`
    
            else 
                echo "[ SKIP ] There is no subtitle that need renaming"
            fi
    }
    
    process_file() {
            local mkvfilename="$1"
            mkvdir=${mkvfilename%/*}
            mkvname=${mkvfilename%[*}
            # dt=$(date '+%d/%m/%Y %H:%M:%S');
    
            echo "$(date '+%d/%m/%Y %H:%M:%S')" ==================== BEGIN ====================
    
            find "$mkvdir" -type f -name "${mkvname##*/}"*.srt | while read subfilename
            do
                rename_subtitles "$subfilename"
            done
    
            mkvmerge --identify-verbose "$mkvfilename" | grep 'subtitles' | while read subline
            do
                mkvmerge --identify-verbose "$mkvfilename" | grep 'subtitles' | grep 'language:heb' | while read subline
                do
                    extract_subtitles_from_file "$mkvfilename" "$subline"
                done
                remove_subtitles_from_file "$mkvfilename"
            done
            
            echo "$(date '+%d/%m/%Y %H:%M:%S')" ==================== DONE ====================
    }
    
    process_dir() {
            local DIR="$1"
            find "$DIR" -type f -name '*.mkv'  | sort -n | while read mkvfilename
            do
                process_file "$mkvfilename"
            done
    }
    
    # If no directory is given, work in local dir
    if [ "$1" = "" ]; then
            process_dir "$PWD"
    # If parameter is file, extract only subtitles for that file
    elif [ -f "$1" ]; then
            process_file "$1"
    # If parameter is directory, process directory recursively
    else
            process_dir "$1"
    fi
    Quote Quote  



Similar Threads

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