Log in

View Full Version : how to get video resolution to use in batch file?


junglemike
3rd July 2012, 18:16
Hello fellas.

I've written some simple batch script which takes any videos in specific folder, and converts them to format supported by Nokia phone 5800 (with it's Real player).

My programming skills are very very basic, so don't be harsh :)
What I've done so far:
-extracting audio into .wav with ffmpeg
-compress audio with nero aac into .mp4
-create simple avisynth script (video only)
-encode .avs file with 2-pass by Xvid_encraw. Real player
supports only mper4-Asp - so no x264 :-((
-mux compressed video & audio into .mp4 using mp4box.exe
What I need your help with:
Phone's resolution is 640x320, so I would like to implement smart resize option. I already written the algorithm itself using batch. All I need - is to get the the video resolution into my script, so I can manipulate it and calculate optimal output resolution for nokia.
I know it's no problem for .avs file (smth like clip.width) - but what about batch file? How Can I get width and height of video file into batch variable?
set bitrate=300
set my_temp=f:\temp
set resize=464,352
rem Create the avs script
set my_cur_dir=%cd%
for %%f in (*.avi *.flv *.mkv *.mpg *.mp4) do (

Rem Extract audio from avi file using ffmpeg
c:\encode\ffmpeg.exe -i "%my_cur_dir%\%%f" "%my_temp%\%%f.wav"


rem Encode the audio with Nero AAC using 39kbps bitrate
cd /d %my_temp%
c:\encode\neroAacEnc.exe -q 0.19 -if "%my_temp%\%%f.wav" -of "%my_temp%\%%f.wav.mp4"

Rem Create .avs file in temp dir
cd /d %my_cur_dir%
echo directshowsource("%my_cur_dir%\%%f",audio=false^) lanczosresize(%resize%^)>%my_temp%\%%f.avs
cd /d %my_temp%


rem Encode the original video file with xvid (2 passes)
start "encode" /b /low /wait "d:\Program Files\Xvid\xvid_encraw.exe" -progress 100 -nopacked -max_bframes 0 -qtype 0 -vhqmode 1 -i "%my_temp%\%%f.avs" -max_key_interval 100 -pass1 src.pass -quality 5 -nochromame -turbo

start "encode" /b /low /wait "d:\Program Files\Xvid\xvid_encraw.exe" -progress 100 -nopacked -max_bframes 0 -qtype 0 -vhqmode 4 -i "%my_temp%\%%f.avs" -max_key_interval 100 -pass2 src.pass -bitrate %bitrate% -quality 6 -avi "%my_temp%\%%f.avi"

rem mux the encoded video and audio and put it in source folder
cd /d %my_cur_dir%

md out
"c:\MP4Box.exe" -add "%my_temp%\\%%f.wav.mp4" -add "%my_temp%\\%%f.avi" "%my_cur_dir%\out\%%f.mp4"

rem delete all temp files
cd /d %my_temp%
del *.* /q
cd /d %my_cur_dir%
)


cd /d %my_cur_dir%\out
for %%f in (*.*) do move "%%f" "%my_cur_dir%"
cd..
rmdir out
pause

Groucho2004
3rd July 2012, 18:23
You could use the Mediainfo CLI version, dump the video properties into a temporary file and then grep the resolution from it.

junglemike
3rd July 2012, 18:57
Thanks for great tip. Already looking into cli option ... :)

Mug Funky
4th July 2012, 04:35
you can redirect ffmpeg to a temp txt file, then either FOR %%A in blabla (look it up, it's a learning curve), or grep. you should be able to find the grep utility by googling "msys grep.exe".

it's fiddly as all get out, but worth it if you're into batching everything.

von_Runkel
2nd August 2012, 10:34
cd /d %my_temp%
del *.* /q
is very dangerous!
If the CD command fails, you will silently delete all content of the current folder. Which could be your Windows directory!

Always use full paths with destructive commands.

Groucho2004
2nd August 2012, 11:30
cd /d %my_temp%
del *.* /q
is very dangerous!
If the CD command fails, you will silently delete all content of the current folder.
Really? Here's another one: When I drive my car into a wall I end up in hospital. Never expected that. :rolleyes:


You could check if the directory exists before you delete it:
if exist %my_temp% [command]

And yes, using fully qualified paths is a good idea.