Search code examples
excelms-accessvideobatch-fileframe-rate

Addition of output file for renaming video batch


With thanks to David Ruhmann, yesterday I got my batch file working for renaming videos:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

:: Create Empty Folder
rd /Q "%Temp%\Temp" 2>nul & mkdir "%Temp%\Temp"

:: Loop through Folders
pushd "xPath=c:\processing"
for /d %%D in (*) do call :Process "%%~fD"
popd
goto End


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Process <Parent>
:: Folder Name
set "xFolder=%~nx1"

:: Set Sub Folder
if not exist "%~1\VIDEO\" goto :eof
pushd "%~1\VIDEO"

:: Loop through Videos
for /f "delims=" %%A in ('dir *.avi /b') do if exist "%%~fA" (
    set "xDateWritten=%%~tA"
    set "xDateGMT=0000/00/00 00:00:00"
    for /f "tokens=1,2" %%X in ('robocopy . "%Temp%\Temp" "%%~nxA" /TS /FP /NS /NC /NP /NJH /NJS /NDL /L') do set "xDateGMT=%%X %%Y"
    rem Format = FF-FF-YYYYMMDD-HHh-MMm-SSs-FF-FF.ext
    echo %xFolder:~0,2%-%xFolder:~2,2%-!xDateWritten:~6,4!!xDateWritten:~0,2!!xDateWritten:~3,2!-!xDateWritten:~11,2!h-!xDateWritten:~14,2!m-!xDateGMT:~17,2!s-%xFolder:~4,2%-%xFolder:~6,2%%%~xA
    ren "%%~fA" "%xFolder:~0,2%-%xFolder:~2,2%-!xDateWritten:~6,4!!xDateWritten:~0,2!!xDateWritten:~3,2!-!xDateWritten:~11,2!h-!xDateWritten:~14,2!m-!xDateGMT:~17,2!s-%xFolder:~4,2%-%xFolder:~6,2%%%~xA"
)
popd
goto :eof


:End
endlocal
pause

But now I need to add an additional feature. After the renaming takes place, I would like the script to export the new filename followed by the filesize in bytes, and the length of the video to a csv or other excel-compatible filetype. As these are video files, if I could export the framerate and total number of frames as well, that would be great. Not sure if this is even within the scope of a batch file anymore though.


Solution

  • Here is a modification to export the name and size to a csv file.

    Change this

    echo %xFolder:~0,2%-%xFolder:~2,2%-!xDateWritten:~6,4!!xDateWritten:~0,2!!xDateWritten:~3,2!-!xDateWritten:~11,2!h-!xDateWritten:~14,2!m-!xDateGMT:~17,2!s-%xFolder:~4,2%-%xFolder:~6,2%%%~xA
    ren "%%~fA" "%xFolder:~0,2%-%xFolder:~2,2%-!xDateWritten:~6,4!!xDateWritten:~0,2!!xDateWritten:~3,2!-!xDateWritten:~11,2!h-!xDateWritten:~14,2!m-!xDateGMT:~17,2!s-%xFolder:~4,2%-%xFolder:~6,2%%%~xA"
    

    Into this

    set "xFrame=00,00000"
    for /f %%X in ('exiftool -p "$Framerate,$Framecount" "%%~fA"') do set "xFrame=%%~X"
    set "xSize=%%~zA"
    set "xName=%xFolder:~0,2%-%xFolder:~2,2%-!xDateWritten:~6,4!!xDateWritten:~0,2!!xDateWritten:~3,2!-!xDateWritten:~11,2!h-!xDateWritten:~14,2!m-!xDateGMT:~17,2!s-%xFolder:~4,2%-%xFolder:~6,2%%%~xA"
    echo !xName!
    ren "%%~fA" "!xName!"
    echo !xName!,!xSize!,!xFrame!>>C:\RenameOutput.csv
    

    Update:

    To obtain the media file information we will have to use a 3rd party tool. Such as

    1. ExifTool
    2. MediaInfo
    3. FFmpeg

    Based on a quick look at ExifTool and MediaInfo, I like MediaInfo better for its more consistent media file Duration output format. Example of how to parse the information.

    for /f "tokens=2,*" %X in ('mediainfo file.avi ^| find "Duration "') do @echo %Y
    

    Update 2:

    Added exiftool example to the answer.