Emulate pressing the Pause key?

Dell / LATITUDE D610
February 24, 2011 at 08:35:29
Specs: Microsoft Windows XP Professional SP3, 2.13GHz / 1GB
I have a couple programs running on a Windows machine that at times can be quite CPU intensive. One of the processes can time out, and kill a subprocess if the CPU resources aren't available to it. I can get around this by hitting the Pause key when the focus is on the other program. The problem is that I can't always monitor what's going on, and can't hit the Pause key. Is it possible to programmatically emulate a Pause key press?

A bit more detail:
Program 1 is a java application that "wakes up" every hour or so to see if an action is required, and creates subprocesses (which can time out) if so. If not, sleep for another hour.

Program 2 is a batch file that loops through my video directories, and executes ffmpeg to transcode every (valid) file found into mp4 format. I have logic built into the batch file to check the existence of Program 1's subprocesses, and to sleep for 5 minutes if any subprocesses are found. This takes place between one file found and the next. This doesn't always work in time if the video file being transcoded is particularly big.

My idea:
Create Program 3 to monitor Program 1's subprocesses, and "Pause" Program 2 if any are found. When Program 1's subprocesses complete, "Unpause" Program 2. Rinse. Repeat.

I know Windows batch, some WMIC, and some python.

When your only tool is a hammer, every problem looks like a nail.


See More: Emulate pressing the Pause key?

Report •

#1
February 24, 2011 at 09:05:34
I'm not sure what the relationship between these Java subprocesses and these movie files are, but. . .

Better idea: Rewrite program 2 to retest after the sleep
Even better idea: Rewrite program 2 to not loop, and have program 3 kick off program 2 when it notices the subprocesses end. WMI notification queries (available for VBScript and Python) would assist you here.

How To Ask Questions The Smart Way


Report •

#2
February 24, 2011 at 09:59:00
First off, thank you Razor for the reply!

"I'm not sure what the relationship between these Java subprocesses and these movie files are, but. . ."

Well, both the Java program and batch file deal with video. The Java program downloads videos off of my TiVo DVRs, runs another program to fix any anomolies in the audio/video streams, runs another program to detect commercials, runs another program to cut commercials, and finally runs a batch program to transfer the commercial cut video back to a TiVo. For the most part, a "lights out" process. It's the commercial detection that times out, and the subsequent subprocesses don't complete correctly.


"Better idea: Rewrite program 2 to retest after the sleep"

Yep, it do. I have an internal loop that uses TaskList to find the subprocesses. If found, sleep 5 minutes and go back to TaskList loop. If not found, continue on with file processing loop.


"Even better idea: Rewrite program 2 to not loop, and have program 3 kick off program 2 when it notices the subprocesses end. WMI notification queries (available for VBScript and Python) would assist you here."

The problem isn't the number of files, it's the size of (some of) the files. A DVD rip or HD video could take several hours to transcode into the mp4 format. So having Program 3 start Program 2 immediately after Program 1 has completed it's work, Program 2 may not finish it's work before Program 1 "wakes up" again and has more work to do. Extending the time Program 1 sleeps to allow Program 2 to finish wouldn't work either, because Program 2 would see Program 1 isn't doing anything, and would/could go grab another huge file to start on.

But yeah, I'm certainly not adverse to running a VBScript, but I have so little experience in VB, I wouldn't know where to start with actual coding of one. Python would be more comfortable for me, and I do have the pywin32 package available (if needed). So monitoring Program 1's subprocesses isn't too much of a challenge. The challenge is how to deal with Program 2. I don't want to kill it; ffmpeg would leave a half-finished video. :-/

Here's the source to Program 2, which isn't really relevant to the issue at hand, but rather something I'm kinda proud of anyway! :-P

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET HOME=D:\ffmpeg

:: Establish working directories
PUSHD "\\sy-6ba-100\Big Files One\Video\MP4"
SET target_path=%CD%
PUSHD "\\Sy-6ba-100\Big Files Two\F Drive\Video\AVI"
SET source_path=%CD%
SET temp_path=%tmp%\Convert

:: Gather files to convert
FOR /F "delims=" %%A IN ('DIR /B /S *.avi *.mpg *.mpeg *.vlc *.asf *.indeo *.wmv *.viv *.flv *.mov *.divx') DO (
    SET new_path=%%~dpA
    SET new_path=!new_path:%source_path%=!
    SET file_full=%%~nxA
    SET file_name=%%~nA
    IF NOT EXIST "!temp_path!\!file_name!.mp4" (
        IF NOT EXIST "!temp_path!\!file_full!" (
            >NUL COPY "%%A" "!temp_path!"
        )
        
        FOR /F "usebackq" %%B IN (`"MediaInfo.exe --Inform=Video;%%FrameCount%% "!temp_path!\!file_full!""`) DO (
            ECHO.
            ECHO *****************************
            ECHO  Frames to convert: %%B 
            ECHO *****************************
            ECHO.
        )
        
        FOR /F "usebackq" %%B IN (`"MediaInfo.exe --Inform=Video;%%FrameRate%% "!temp_path!\!file_full!""`) DO (
            SET fps=%%B
        )

        FOR /F "usebackq" %%B IN (`"MediaInfo.exe --Inform=Audio;%%BitRate%% "!temp_path!\!file_full!""`) DO (
            SET /A orig_ab=%%B
            SET /A orig_ab=!orig_ab!/1000
        )

        FOR /F "usebackq" %%B IN (`"MediaInfo.exe --Inform=Audio;%%Format%% "!temp_path!\!file_full!""`) DO (
            IF /I "%%B" EQU "PCM" (
                SET /A orig_ab=192
            )
        )

        FOR /F "usebackq" %%B IN (`"MediaInfo.exe --Inform=Audio;%%SamplingRate%% "!temp_path!\!file_full!""`) DO (
            IF /I %%B LSS 48000 (
                SET ar=44100
            )
            IF /I %%B GEQ 48000 (
                SET ar=48000
            )
        )

        SET ab=
        FOR %%B IN (32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640) DO (
            IF NOT DEFINED ab (
                IF /I %%B GEQ !orig_ab! (
                    SET /A ab=%%B
                )
            )
        )
        
        CALL :Check_kmttg
        SET input_parm=-threads !threads! -i "!temp_path!\!file_full!"
        SET audio_parm=-acodec ac3 -ab !ab!k -ar !ar!
        SET video_parm=-vcodec libx264 -vpre veryfast -crf 22 -level 41 -r !fps!
            
        ECHO.
        ECHO ffmpeg !input_parm! !audio_parm! !video_parm! -y "!temp_path!\!file_name!.mp4"
        ECHO.
        ffmpeg !input_parm! !audio_parm! !video_parm! -y "!temp_path!\!file_name!.mp4"

        IF !ERRORLEVEL! EQU 0 (
            CD /D !temp_path!
            FOR /F "delims=" %%C IN ('DIR /B "!temp_path!\!file_full!"') DO (
                SET old_size=%%~zC
            )
            FOR /F "delims=" %%C IN ('DIR /B "!temp_path!\!file_name!.mp4"') DO (
                SET new_size=%%~zC
            )
            >>File.sizes.csv ECHO.!new_path!!file_name!, !old_size!, !new_size!
            ECHO MOVE "!temp_path!\!file_name!.mp4" "%target_path%!new_path!"
            CD /D %source_path%
            qtfaststart.py "!temp_path!\!file_name!.mp4"
            MOVE /Y "!temp_path!\!file_name!.mp4" "%target_path%!new_path!"
            DEL /Q "!temp_path!\!file_full!"
            REN "%%~A" "!file_full!.done"
        )
        REM PAUSE
    )
)
POPD
POPD

EXIT /B

:Check_kmttg
SET threads=2
FOR /F "tokens=* delims= " %%H IN ('TASKLIST /FI "STATUS eq RUNNING"^|FINDSTR /I "curl tivodecode comskip VIDEOR~1"') DO (
	IF "%%H" NEQ "" (
        ECHO Pausing 5 minutes
        SLEEP 300
        GOTO Check_kmttg
	)
)
GOTO :EOF

When your only tool is a hammer, every problem looks like a nail.


Report •

#3
February 24, 2011 at 10:12:01
So what is the ultimate goal? To make sure program 2 doesn't steal too much CPU from program 1's childern? If that's the case, try starting program 2 with a lower priority, say BELOWNORMAL or even LOW.

Suggested reading: START /?

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
February 24, 2011 at 11:20:50
That's a fine idea! I'll have to test that.

And wow, the formatting of post #2 isn't very conducive for reading. My apologies. I was hoping only the text in the pre tags were not wrapped, and the "conversational" text was...

When your only tool is a hammer, every problem looks like a nail.


Report •

#5
March 1, 2011 at 09:05:53
Follow up:

I ended up using the following to set Program 1's subprocesses to High priority:

@ECHO OFF
SETLOCAL
TITLE %~n0

:Check_kmttg
FOR %%H IN (curl tivodecode comskip VIDEOR~1) DO (
    >NUL 2>&1 wmic process where "name='%%H.exe' and priority!=13" setpriority 128
)
SLEEP 300
GOTO Check_kmttg

When your only tool is a hammer, every problem looks like a nail.


Report •

Ask Question