Search code examples
windowsvideobatch-filecmdmediainfo

Renaming my video files as per the resoltuion using batch


I would like to rename my video files as per the resolution they are in, e,g for a video 'bla bla.mp4' in 1080p, I would like to rename it to 'bla bla [H.264 1080p]. The script should automatically be able to detect the resolution of the video, and also if the file has been already renamed it should not rename it.I wasn't able to find a way to check for the resolution, so I tried to use this for 1080p files:

    FOR /r %%a in (*.mp4) DO (IF EXIST *[H.264*.mp4 (
    ECHO Already done) 
    ELSE (
    REN "%%~a" "%%~na [H.264 1080p].mp4"))

But what it does is it checks for the same file again and again which has already been renamed and therefore the reply always is 'Already done'.


Solution

  • This question is beyond the scope of an SO question. Nevertheless I will answer it, because today is sunday.

    • download and install command line version
    • add the path to the mediainfo binaries to your system or user PATH environment variable
    • copy the rename script, replace the path to your video folder, there is a safety echo before the rename command, remove it if the output looks good
    • the script tests for already-exists and already-processed files (suggested by Peter Wright)

    rename script:

    @echo off & setlocal
    cd X:\video\path
    for /f "delims=" %%i in ('dir /b /a-d *.mp4') do (
        set "fnameo=%%~ni"
        set "fnamee=%%~xi"
        set "Height="
        for /f "delims=" %%j in ('mediainfo "--Inform=Video;%%Height%%" "%%~i"') do set "Height=%%j" 
        setlocal enabledelayedexpansion
        call set "selftest=%%fnameo:[H.264 !Height!p]=%%"
        if "!selftest!" equ "!fnameo!" if not exist "!fnameo! [H.264 !Height!p]!fnamee!" (
            echo rename "!fnameo!!fnamee!" "!fnameo! [H.264 !Height!p]!fnamee!"
        )
        endlocal
    )
    

    output example:

    rename "Handbrake.0.9.8.mp4" "Handbrake.0.9.8 [H.264 800p].mp4"
    rename "Hybrid.2012.10.21.1.mp4" "Hybrid.2012.10.21.1 [H.264 800p].mp4"
    rename "Womble.mp4" "Womble [H.264 1080p].mp4"