0

I am trying to get a '# of file processed' within my script but can't get there any only total # 'found' and also disable ffmpeg to just do the same pre-scan as an inventory or check of files with specific codecs.

The problem seems to be with the nested Disabled Delayed Expansion, but I've been learning batch for the last 3 months and searching and comparing/testing scripts for weeks each day but I can't seem to break through this.

And/or using Dbenham's Return.bat call. But I'm hessitant about return.bat as it seems such a large processing/time overhead to just accumulate a counter.

Script does the the following:

  1. Commented out: Logging function process which creates a directory a file for each run
  2. Sets destination Root drive & folder: "SET drive=T:\4k.temp"
  3. Sets the \bin directory and initial variables: FilesFound=0
  4. Runs for loop to find all mkv & mp4 files
  5. Inside Loop: Increments variable FilesFound+=1 and sets paths and filename
  6. Nested for loop analyzes each file's properties to match 'codec_name' with the variable set near the top "Codec1=dts". If matched, sets SkipCodec=1

    Set EnableDelayedExpansion
  7. For each file if it's SkipCodec matched 1 it would (needed to nest mkdir underneath DisableDelayedExpansion) create a directory that matched it's current folder tree but on the destination root, set at the top.

    (call :EncoderCounts - Testing/attempted to learn how to user call function to 'carry' the variable over to the 'end results' section)
  8. Processes the FFMpeg command or Sets FilesEncoded Counter

    Endlocal
  9. Echo's Results with variables

Is anyone able to help me break through this barrier??

setlocal
rem #######1-Destination Drive #############
SET drive=T:\4k.temp
    setlocal EnableExtensions EnableDelayedExpansion
    SET "Codec1=dts"
    SET "FINALCOMMAND="
    setlocal EnableExtensions DisableDelayedExpansion
        SET "ProgramFolder=C:\FFmpeg\bin"
        SET "ProbeOptions=-v quiet -select_streams a:0 -show_entries "stream^^=codec_name" -of json"
        SET "FilesFound=0" & SET "FilesEncoded=0" & SET "FullFileName=" & SET "output="
        
        for /F "delims=" %%I in ('dir *.mkv *.mp4 /A-D-H /B /S 2^>nul') do (
            @ECHO ================Next File: %%I =============
            SET "FullFileName=%%I" &    SET "output=%drive%%%~pI%%~nxI"
            SET /A FilesFound+=1 & SET "AudioCodec=" & SET "SkipCodecs="
            for /F "eol={ tokens=1,2 delims=,:[ ]{} " %%B in ('""%ProgramFolder%\ffprobe.exe" %ProbeOptions% "%%I""') do (
                if "%%~B" == "codec_name" (
                    if not defined AudioCodec (
                        SET "AudioCodec=%%~C"
                    )
                    if "%%~C" == "%Codec1%" (set "SkipCodecs=1"
                    )
                )
            )
            setlocal EnableExtensions EnableDelayedExpansion
                if !SkipCodecs! == 1 (
                    setlocal EnableExtensions DisableDelayedExpansion
                        MKDIR "%drive%%%~pI" 2>null
                    endlocal
                    SET "FINALCOMMAND=ffmpeg -n -hide_banner -loglevel quiet -stats -i "!FullFileName!" -map 0:v -map 0:a:0 -c:v copy -c:a:0 ac3 -b:a:0 640k "!output!""
                        !FINALCOMMAND!&SET /A FilesEncoded+=1
                    ECHO #####COMPLETE############
                ) else ( ECHO #####NOT PROCESSING ############)
                ECHO[
            endlocal
        )
    if %FilesFound% == 1 ( SET "PluralS=" ) else set "PluralS=s"

    @ECHO *********************************************************
    ECHO re-encoded %FilesEncoded% of %FilesFound% video file%PluralS%.
    endlocal
    GOTO :END
:END
endlocal
endlocal
exit /b
Compo
  • 36,585
  • 5
  • 27
  • 39
hsgg4
  • 31
  • 1
  • 5
  • 1
    Could you please consider posting a [mcve]. – Squashman Feb 02 '21 at 01:39
  • As @Squashman said, your example is much too complex. But probably you are searching not for `return.bat`, rather a simple `FOR` endlocal technique is enough, examine the block, beginning with [for /f "delims=" %%p in ("!insert!%%~L!line!") do (](https://stackoverflow.com/a/18063366/463115) – jeb Feb 02 '21 at 07:31
  • Thanks Jeb, I am still checking that out – hsgg4 Feb 02 '21 at 14:03
  • Thanks Jeb you found it!! A small question, it seems the using a CALL makes all the ffmpeg processes run first, then display all the ECHOs for the majority of the script, see this picture. [newline] [Command Prompt of fixed script](https://https://i.imgur.com/v1LrhRT.jpg) – hsgg4 Feb 02 '21 at 16:45
  • @hsgg4, the comment area isn't for additional support beyond your original question. If you have a new issue, then create a new on topic, question with a [mcve] of your code, and sufficient supporting and debugging information for us to assist you. – Compo Feb 02 '21 at 21:08

1 Answers1

0

Jeb's solution worked, here is the working copy:

SETlocal
SET drive=T:\4k.temp
SET string=%CD%
    SETlocal EnableDelayedExpansion
        for %%* in (.) do SET LogFolder=%%~n*
        MD "%drive%\Logs\%LogFolder%"
        SET "LOGFILE=%drive%\Logs\%LogFolder%\Log.log"
    endlocal & SET LOGFILE=%LOGFILE%
        SETlocal EnableExtensions EnableDelayedExpansion
        (call ^:logit)>>"%LOGFILE%"
        :logit
        SET "FINALCOMMAND="
        SET "Codec1=dts"
            SETlocal EnableExtensions DisableDelayedExpansion
                SET "ProgramFolder=C:\Program Files\FFmpeg-v2020\bin"
                SET "ProbeOptions=-v quiet -select_streams a:0 -show_entries "stream^^=codec_name" -of json"
                SET "FilesFound=0" & SET "FilesEncoded=0" & SET "output="
                for /F "delims=" %%I in ('dir *.mkv *.mp4 /A-D-H /B /S 2^>nul') do (
                    @ECHO ================Next File: %%I ========================
                    SET "output=%drive%%%~pI%%~nxI" & SET /A FilesFound+=1 & SET "AudioCodec=" & SET "SkipCodecs="
                    for /F "eol={ tokens=1,2 delims=,:[ ]{} " %%B in ('""%ProgramFolder%\ffprobe.exe" %ProbeOptions% "%%I""') do (
                        if "%%~B" == "codec_name" (
                            if not defined AudioCodec (
                                SET "AudioCodec=%%~C"
                            )
                            if "%%~C" == "%Codec1%" (SET "SkipCodecs=1" &ECHO   This is Codec1:         %Codec1%
                                )
                            )
                        )
                    )
                    SETlocal EnableExtensions EnableDelayedExpansion
                        if !SkipCodecs! == 1 (
                            SETlocal EnableExtensions DisableDelayedExpansion
                                MKDIR "%drive%%%~pI" 2>null
                            endlocal
                        )
                        if !SkipCodecs! == 1 (
                                ECHO    Processing ffmpeg x265 to make AC3 with:    "!Codec1!"
                            SET "FINALCOMMAND=ffmpeg -n -hide_banner -loglevel quiet -stats -i "%%I" -map 0:v -threads 6 -map 0:a:0 -map 0:s? -c:v copy -c:a copy -c:a:0 ac3 -b:a:0 640k "!output!""
                            !FINALCOMMAND!
                            ECHO #####COMPLETE################### &ECHO[ &ECHO[
                            if not %FilesEncoded% == 0 ECHO     This one was a failure. Count Encoded so far: "%FilesEncoded%"
                            
                            for /F "delims=" %%p in ("!SkipCodecs!") do (
                            ENDLOCAL
                                SET /A FilesEncoded=%%p+FilesEncoded
                            )
                        ) ELSE ( ECHO #####)
                )
                if %FilesFound% == 1 ( SET "PluralS=" ) else SET "PluralS=s"
                @ECHO ***************************************************************************************
                ECHO Re-encoded !FilesEncoded! of %FilesFound% video file%PluralS%.
            endlocal
        endlocal
endlocal
exit /b
hsgg4
  • 31
  • 1
  • 5