Search code examples
batch-filefor-loopffmpegwindows-8.1affinity

Rotate affinity values in a batch script for loop


In windows 8.1, I'm converting my videos to HEVC using FFmpeg, using this simple batch script:

md hevc

start /affinity 0x7 for %%k in (*.mkv *.mp4) do ffmpeg -i "%%k" -map 0 -c copy -c:v libx265 -y "hevc\%%~nk.mkv"

I'm using the Start /affinity to limit FFmpeg to 3/4 threads, otherwise my computer gets chocked. It works fine, except it uses the same affinity value for an entire folder, and I'd rather rotate the affinity values between 0x7, 0xb, 0xd and x0e, so that it changes the active cores each time another video starts. I've tried putting the start /affinity in different positions in the command line, and I've tried running a loop within a loop, but nothing I've tried has worked. Any ideas?


Solution

  • @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET /a selection=0
    for %%k in (*.mkv *.mp4) do  (
     FOR %%a IN (0:0x7 1:0xb 2:0xd 3:0xe) DO FOR /f "tokens=1,2delims=:" %%c IN ("%%a") DO IF !selection!==%%c ECHO(start "%%k" /affinity %%d ffmpeg -i "%%k" -map 0 -c copy -c:v libx265 -y "hevc\%%~nk.mkv"
     SET /a selection=(1+selection^) %% 4
    )
    GOTO :EOF
    

    This should do as you ask. The ffmpeg lines are echoed for testing purposes. To activate the process, remove the ECHO(

    I've included a "%%k" after the start to act as a window-title as sart uses the first quoted string found on the line.

    The process simply cycles the variable selection from 0 to 3, changing it for each ffmpeg despatched. The delayedexpansion mechanism is used to provide access to the run-time value of selection