View Full Version : Get width or height and add it to file's name with PowerShell ?
Music Fan
25th February 2019, 17:45
Hi,
for some videos, I'd like to add their width or height in their file's name but I can't figure out how to make it (if possible) with PowerShell.
I know that some metadatas are usable to be added in file's name ; this batch command line works to add the "last modification date" at the end of all file's name in a folder ;
get-childitem -Path "F:\vid\" | ForEach-Object {$_ | Rename-Item -NewName {$_.Basename + ' ' + ("{0:yyyy-MM-dd HH\hmm}" -f $_.LastWriteTime) + $_.Extension }}
And I was wondering if the same kind of process could work with other metadatas, as video resolution.
I tried this on an mp4 (Windows 7 can display its resolution in the bottom part of the explorer when I click on it, thus I guess there should be a way to recover this information) but it doesn't work ;
get-childitem -Path "F:\vid\" | ForEach-Object {$_ | Rename-Item -NewName {$_.Basename + ' ' + ("{0:0-9}" -f $_.FrameHeight) + $_.Extension }}
It's not refused but it only adds a space, no resolution. I guess the error is in the ("{0:0-9}" -f $_.FrameHeight) part.
I tried several variants (0:^[1-3000], 0:^0-9) but the result is always the same.:scared:
Does anybody know how to achieve this ?
Or maybe with ffprobe ?
Thanks.
manolito
25th February 2019, 18:08
Hello,
I do such stuff all the time with simple batch files and the CLI version of MediaInfo.
Please tell me your desired target file name format, and I will hack something together for you. I suppose you want the capability to do this for all video files in a folder. Do you want to delete the original files and just keep the renamed files, or do you want to keep the original files, too?
Cheers
manolito
Music Fan
25th February 2019, 18:14
Thanks, I'd like to keep the original files, thus export the new ones in another folder.
But you can give both scripts, it may be useful ;)
For the target file name format : original name + space + resolution (width or height or both with a point between them).
For example ;
my video.mp4
becomes
my video 1280.720.mp4
:thanks:
manolito
25th February 2019, 18:38
Alright, I'll do it later tonite... :)
Fortunately I don't need to do this from scratch, I can reuse most of the code. But I just started to do my tax return for last year, I don't want to interrupt this right now.
Music Fan
25th February 2019, 19:19
No problem, I'm not in a hurry.
Actually, I believe that i don't need to keep the original files ; that's only a name change after all, not a re-encoding.
manolito
25th February 2019, 23:41
Alright, here we go:
https://www.sendspace.com/file/5cytwp
Extract the 2 files into a folder of your choice, then drag & drop (or copy & paste) the source file(s) on the "Rename_Resolution.bat" file.
Some notes:
The "Rename_Resolution.bat" file works like a droplet. Highlight one or many source files from a folder and drag & drop them on the batch file. You can also create a link to the batch file on your desktop to make it easier.
By default the source files are renamed. You can edit the batch file to change the default behavior to copying the source files.
I used the older MediaInfo version 18.05. Zenitram changed the interface considerably after this version. I have no idea if newer versions will work, try it for yourself.
You can easily expand the renaming capabilities by adding additional MediaInfo properties (like frame rate or aspect ratio). The MediaInfo CLI download comes with comprehensive documentation about the supported file properties.
Cheers
manolito
Music Fan
26th February 2019, 17:04
Thanks, this works well !
This gives me an idea : is there a way to add an i (for interlaced) or a p (for progressive) after the resolution (without space) ? :o
1280.720p, 720.576i ...
It's not that important but that would be cool ;)
:thanks:
manolito
26th February 2019, 18:10
Sure, no problem... :p
https://www.sendspace.com/file/n5rgn0
Music Fan
26th February 2019, 19:58
Great!
:thanks:
I noticed a strange thing with one avi file : the resolution has been added but the end of the name was erased :confused:
But I tested with your second version, no more problem :)
I still have a request :o : sometimes, there is no information about scan type and nothing is added after resolution.
As it probably means in (nearly) all cases that it's progressive, could you add a "p" at the end anyway ?
;)
manolito
26th February 2019, 20:21
This is hard to debug without a source file sample. In the latest version I changed the way how the MediaInfo output is exported into an environment variable. The old version used intermediate txt files, the new version does it directly with a FOR /F command. I had hoped that this would improve the speed, but it does not. If it fixes the output with one of your source files, all the better...
For files where MediaInfo does not report the ScanType value, this could have many reasons. New file formats might be MBAFF instead of Interlaced, but I think this is a special "Interlaced" format. It would be nice if you could upload such a file where my routine fails (I guess you know how to cut out a short sample...). Just blindly adding "p" seems a bit risky to me.
Cheers
manolito
Music Fan
27th February 2019, 08:43
Ok, but if it's MBAFF, we could maybe add in the script ;
IF !%type%==!MBAFF SET type=i
and "if no scan type, set p" (but I don't know how to make it).
manolito
27th February 2019, 18:14
OK, check out this one:
https://www.sendspace.com/file/24j1w6
For MBAFF and PAFF it will report "i", if there is no ScanType it will report "p".
Music Fan
28th February 2019, 09:16
Thanks !
In some cases, I may need also the framerate, thus I could edit the file to create a second bat : can you give just the line to be added in the script ? No need to post another bat.
Suggestion : if there is nothing after the point, write only what is before, thus for 30.000 FPS, write only 30, but if i's 29.970, write 29.97.
In other words, remove everything atfer the last digit which is not a 0, but only after the point of course (if possible), not to have 3 instead of 30.
Sorry to be boring :o
;)
manolito
28th February 2019, 18:25
Do you need the frame rate combined with the frame size and scan type in one file name, or do you want a second batch file which only adds the frame rate to the file name?
Luckily MediaInfo can report the raw frame rate exactly in the format you want, so this should not be a problem (string formatting is not a strong point of the batch language - it could be done, though).
One problem might be if you feed a file with variable frame rate to the batch file. For such VFR files MediaInfo does not report a frame rate at all, it just reports "VFR". There are 2 possible workarounds:
1. Older MediaInfo versions behaved differently, they did report a frame rate for VFR files. Not sure if I can find such an older version, and an older version might have problems with newer formats.
2. AVSMeter also reports frame rates for VFR files. But here I would have to apply some nasty string formatting tricks...
Cheers
manolito
manolito
28th February 2019, 22:22
I went ahead and made 2 batch files for adding the frame rate to the file name. For now I assumed that you want a separate batch file for the frame rate thing, but of course this can be changed.
Download link:
https://www.sendspace.com/file/0wvdnh
First of all my assumption that MediaInfo can already manage the string format to your needs was wrong. The documentation is incorrect. So I had to apply my nasty batch file tricks, wasn't so hard after all.
The first method is to use the older MediaInfo version 0.7.58 which still reports fps values for VFR sources. It is from 2012, so I am not sure how modern formats like HEVC are handled. Of course you can also try this batch file with a current MediaInfo version, but then you will not get any fps value for VFR sources.
The other method uses AVSMeter to determine the frame rate. Of course this requires that you have AviSynth installed on your computer. The DSS2 source filter must also be installed in the "AviSynth\Plugins" folder.
Cheers
manolito
Music Fan
1st March 2019, 09:18
Do you need the frame rate combined with the frame size and scan type in one file name
Yes ;)
One problem might be if you feed a file with variable frame rate to the batch file. For such VFR files MediaInfo does not report a frame rate at all, it just reports "VFR".
That's ok, VFR is enough. If I wanna know more about the file, I will open it in Mediainfo.
Anyway, I use very rarely VFR encodings.
manolito
1st March 2019, 18:34
That's ok, VFR is enough. If I wanna know more about the file, I will open it in Mediainfo.
This won't help you, the MediaInfo GUI also does not report any frame rate for VFR files... :eek:
OK, I will add the frame rate routine to the original batch file. I will add the older MediaInfo version 0.7.58 in a separate subfolder so you can switch between the versions. Will upload in a few hours.
Music Fan
1st March 2019, 18:43
Thanks.
This won't help you, the MediaInfo GUI also does not report any frame rate for VFR files... :eek:
It shows an average frame rate (maybe not always).
manolito
1st March 2019, 20:47
Only if the container is not MKV. Have a look at this little dispute I had with Zenitram a while ago:
https://forum.doom9.org/showthread.php?p=1739434#post1739434
Anyways, the new version is ready for download:
https://www.sendspace.com/file/fkqmav
Have fun
manolito
Music Fan
2nd March 2019, 09:37
Great, thanks !
I see that you didn't put this line ;
IF NOT DEFINED height GOTO next_file
It's done for width and type but not height, I added it.
Music Fan
2nd March 2019, 10:03
By the way, you forgot to add VFR in the name for VFR files.
manolito
2nd March 2019, 17:58
Great, thanks !
I see that you didn't put this line ;
IF NOT DEFINED height GOTO next_file
It's done for width and type but not height, I added it.
No, I did this on purpose (effective coding). If MediaInfo does not report a value for Width then the file is not a video. No reason to check the Height then, one check is all you need.
By the way, you forgot to add VFR in the name for VFR files.
No, this was also on purpose. The FrameRate_Mode is not reported under FrameRate, it has its own parameter. I was too lazy to check it, because if you don't get a value for FrameRate then the Mode will always be VFR.
Here is a "universal" template for my needs, it also reports the AspectRatio and the FrameRate_Mode. Under the label "outname" you can easily modify the format of the output to your needs, should be easy... :cool:
https://www.sendspace.com/file/hof8bz
Cheers
manolito
Music Fan
2nd March 2019, 20:26
Thanks.
I achieved to add VFR for VFR files, but the script also adds CFR for CFR files while I didn't specify it ; is there a way not to write anything for the FrameRate_Mode when it's CFR ?
For example ;
FOR /F %%a in ('MediaInfo.exe "--Inform=Video;%%FrameRate_Mode%%" %in%') DO SET fr_mode=%%a
IF "%fr_mode%"=="Variable" SET type=VFR
IF "%fr_mode%"=="Constant" SET type=
I also tried ;
IF "%fr_mode%"=="Constant" DO NOT SET type
but this adds CFR anyway.:confused:
manolito
2nd March 2019, 21:17
The FrameRate_Mode parameter returns CFR or VFR, not Constant or Variable. Use these lines:
FOR /F %%a in ('MediaInfo.exe "--Inform=Video;%%FrameRate_Mode%%" %in%') DO SET fr_mode=%%a
SET fr_mode= %fr_mode%
IF "%fr_mode%"==" CFR" SET fr_mode=
and in the "outname" line remove the space character between %fps% and %fr_mode%. Should look like this:
%fps%%fr_mode%
If you don't do this your new file name will have an extra space before the dot for CFR files.
Music Fan
3rd March 2019, 09:36
Thanks, I also had to remove 2 spaces : in front of %fr_mode% (second line) and in front of CFR (third line).
Otherwise, CFR was added anyway and a space was added in front of VFR (despite the fact that I removed the space in the "outname" line), now it's perfect ;)
Music Fan
9th June 2023, 13:49
Hi manolito,
a few years later, I'm trying to do something else based on your script : change the extension using MediaInfo because I have some video files with wrong extensions and would like to correct them.
I tried to modify your script and made this ;
@ECHO off
IF !%1==! GOTO syntax
SET Work_Drive=%~d0
SET Work_PATH=%~p0
%Work_DRIVE%
CD "%Work_PATH%"
FOR /F %%a in ('MediaInfo.exe "--Inform=General;%%Format%%" %in%') DO SET Format=%%a
IF NOT DEFINED Format GOTO next_file
FOR /F %%a in ('MediaInfo.exe "--Inform=General;%%Format profile%%" %in%') DO SET ForProf=%%a
IF "%Format%"=="MPEG-TS" SET Format=ts
IF "%Format%"=="Matroska" SET Format=mkv
IF "%Format%"=="Flash Video" SET Format=flv
IF "%Format%"=="AVI" SET Format=avi
IF "%Format%"=="MPEG-4" GOTO FormProf
:FormProf
IF "%ForProf%"=="Base Media" SET Format=mp4
IF "%ForProf%"=="QuickTime" SET Format=MOV
:outname
SET out_name=%in_name%.%Format%
RENAME %in% "%out_name%"
REM COPY /B %in% "%in_path%%out_name%" >NUL
:next_file
SHIFT
IF NOT !%1==! GOTO syntax
GOTO :EOF
:syntax
CLS
ECHO.
Echo Press a key to exit...
PAUSE >nul
I put it with a .bat extension in my MediaInfo folder but it doesn't work, it actually opens MediaInfo but nothing happens with the extension of the file I dropped on the bat.
I guess something's wrong with my code.
mp4 and mov have both the same format for MediaInfo (MPEG-4), that's why I had to redirect them to "Format profile" (with "GOTO FormProf") which helps to distinguish them.
Thanks for your help.;)
Music Fan
10th June 2023, 12:59
By the way, if I put it in the same folder than the CLI version you gave me in 2019, I see briefly a DOS command window (as with your script) but nothing happens either on the video file.
Emulgator
12th June 2023, 08:22
Since all the sendspace links are dead now, can you put the scripts back up here in doom9 as plain code snippets ?
That would make it possible to troubleshoot & modify, and it would be nice to have them back available again.
Music Fan
12th June 2023, 11:09
Yes, here is the code that still works with a 2019 CLI MediaInfo version to add resolution in name ;
@ECHO off
IF !%1==! GOTO syntax
SET Work_Drive=%~d0
SET Work_PATH=%~p0
%Work_DRIVE%
CD "%Work_PATH%"
:loop
SET in="%~1"
SET in_path=%~dp1
SET in_name=%~n1
SET extension=%~x1
SET width=
SET height=
Set type=
SET fr_mode=
SET fps=
FOR /F %%a in ('MediaInfo.exe "--Inform=Video;%%Width%%" %in%') DO SET width=%%a
IF NOT DEFINED width GOTO next_file
FOR /F %%a in ('MediaInfo.exe "--Inform=Video;%%Height%%" %in%') DO SET height=%%a
FOR /F %%a in ('MediaInfo.exe "--Inform=Video;%%ScanType%%" %in%') DO SET type=%%a
IF NOT DEFINED type SET type=p
IF "%type%"=="Progressive" SET type=p
IF "%type%"=="Interlaced" SET type=i
IF "%type%"=="MBAFF" SET type=i
IF "%type%"=="PAFF" SET type=i
FOR /F %%a in ('MediaInfo.exe "--Inform=Video;%%FrameRate_Mode%%" %in%') DO SET fr_mode=%%a
SET fr_mode=%fr_mode%
IF "%fr_mode%"=="CFR" SET fr_mode=
FOR /F %%a in ('MediaInfo.exe "--Inform=Video;%%FrameRate%%" %in%') DO SET fps=%%a
IF NOT DEFINED fps GOTO outname
:format_fps
IF %fps:~-1%==0 SET fps=%fps:~0,-1%& GOTO format_fps
IF %fps:~-1%==. SET fps=%fps:~0,-1%
:outname
SET out_name=%in_name% %width%.%height%%type%%fps%%fr_mode%
REM ____________________________________________________________________________________________________________
REM Use next command to rename the source file(s), use the command after the next one to copy the source file(s)
REM ____________________________________________________________________________________________________________
RENAME %in% "%out_name%%extension%"
REM COPY /B %in% "%in_path%%out_name%%extension%" >NUL
:next_file
SHIFT
IF NOT !%1==! GOTO loop
GOTO :EOF
:syntax
CLS
ECHO.
ECHO.
ECHO ERROR: No Input File Specified...
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO Syntax is: Rename_Properties.bat Input_file(s)
ECHO.
ECHO.
ECHO You can also drag and drop or copy and paste one
ECHO or multiple input file(s) on the Rename_Properties.bat file.
ECHO.
ECHO.
ECHO Hint: Create a shortcut to Rename_Properties.bat on the desktop.
ECHO Makes it easy to drop input files on the desktop icon...
ECHO.
ECHO.
ECHO.
ECHO.
Echo Press a key to exit...
PAUSE >nul
I didn't use %extension% at the RENAME and REM COPY lines in my new script (see a few posts above), that's maybe the problem but I believed my trick could work.
Emulgator
14th June 2023, 23:41
Many thanks, Music Fan !
Music Fan
28th June 2023, 19:34
Hi Emulgator, did you try to modifiy my script (based on manolito's work) to find the problem ?
Emulgator
28th June 2023, 22:59
Not yet, sitting deep in repairs...
Music Fan
26th July 2023, 18:54
Hi, I made some modifications and there is a little progress.
But curiously, the code only works with ts, avi and mkv but not with mp4, mov, wmv and flv for which extension is removed :confused:
Same problem with a more recent MediaInfo CLI version.
@ECHO off
IF !%1==! GOTO syntax
SET Work_Drive=%~d0
SET Work_PATH=%~p0
%Work_DRIVE%
CD "%Work_PATH%"
:loop
SET in="%~1"
SET in_path=%~dp1
SET in_name=%~n1
SET extension=
FOR /F %%a in ('MediaInfo.exe "--Inform=General;%%Format%%" %in%') DO SET Format=%%a
IF NOT DEFINED Format GOTO next_file
FOR /F %%a in ('MediaInfo.exe "--Inform=General;%%Format profile%%" %in%') DO SET Format profile=%%a
IF "%Format%"=="MPEG-TS" SET extension=ts
IF "%Format%"=="Matroska" SET extension=mkv
IF "%Format%"=="Flash Video" SET extension=flv
IF "%Format%"=="AVI" SET extension=avi
IF "%Format%"=="MPEG-4" GOTO FormProf
:FormProf
IF "%Format profile%"=="Base Media" SET extension=mp4
IF "%Format profile%"=="Base Media / Version 2" SET extension=mp4
IF "%Format profile%"=="QuickTime" SET extension=MOV
:outname
SET out_name=%in_name%
RENAME %in% "%out_name%.%extension%"
REM COPY /B %in% "%in_path%%out_name%.%extension%" >NUL
:next_file
SHIFT
IF NOT !%1==! GOTO syntax
GOTO :EOF
:syntax
CLS
ECHO.
Echo Press a key to exit...
PAUSE >nul
Music Fan
5th August 2023, 11:16
I still made a little modification and now the extension is not removed anymore for mp4, mov, wmv and flv files but is not corrected either, very strange :confused:
@ECHO off
IF !%1==! GOTO syntax
SET Work_Drive=%~d0
SET Work_PATH=%~p0
%Work_DRIVE%
CD "%Work_PATH%"
:loop
SET in="%~1"
SET in_path=%~dp1
SET in_name=%~n1
SET extension=%~x1
FOR /F %%a in ('MediaInfo.exe "--Inform=General;%%Format%%" %in%') DO SET Format=%%a
IF NOT DEFINED Format GOTO next_file
FOR /F %%a in ('MediaInfo.exe "--Inform=General;%%Format profile%%" %in%') DO SET Formatprofile=%%a
IF "%Format%"=="MPEG-TS" SET extension=.ts
IF "%Format%"=="Matroska" SET extension=.mkv
IF "%Format%"=="Flash Video" SET extension=.flv
IF "%Format%"=="AVI" SET extension=.avi
IF "%Format%"=="Windows Media" SET extension=.wmv
IF "%Format%"=="MPEG-4" GOTO FormProf
:FormProf
IF "%Formatprofile%"=="Base Media" SET extension=.mp4
IF "%Formatprofile%"=="Base Media / Version 2" SET extension=.mp4
IF "%Formatprofile%"=="QuickTime" SET extension=.MOV
:outname
SET out_name=%in_name%
RENAME %in% "%out_name%%extension%"
REM COPY /B %in% "%in_path%%out_name%%extension%" >NUL
:next_file
SHIFT
IF NOT !%1==! GOTO loop
GOTO :EOF
:syntax
:syntax
CLS
ECHO.
ECHO.
ECHO ERROR: No Input File Specified...
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO Syntax is: rename_extension_k.bat Input_file(s)
ECHO.
ECHO.
ECHO You can also drag and drop or copy and paste one
ECHO or multiple input file(s) on the rename_extension_k.bat file.
ECHO.
ECHO.
ECHO Hint: Create a shortcut to rename_extension_k.bat on the desktop.
ECHO Makes it easy to drop input files on the desktop icon...
ECHO.
ECHO.
ECHO.
ECHO.
Echo Press a key to exit...
PAUSE >nul
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.