user780756
user780756

Reputation: 1484

How to return width and height of a video/image using ffprobe & batch

I need to get the width and height of an image file using ffprobe and need to store it in variables using batch (Windows) so I can later use those values.

I tried to do this,

@echo off
for /f "tokens=1-2" %%i in ('ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=width,height %1') do set W=%%i & set H=%%j
echo %W%
echo %H%

But fails to execute with

Argument '_' provided as input filename, but 's' was already specified.

p.s. I also tried imagemagick identify in a similar way, but it seems that identify has a bug when returning height for GIF files

Upvotes: 1

Views: 3594

Answers (4)

slyfox1186
slyfox1186

Reputation: 350

Outputs the file's name and it's width + height to the terminal.

#!/usr/bin/env bash

clear

name="$(find ./ -type f -iname '*.mp4' | sed 's/^..//g')"

for f in "$(echo $name | tr ' ' '\n')"
do
    for i in ${f[@]}
    do
        dimensions="$(ffprobe -v error -select_streams v -show_entries stream=width,height -of csv=p=0:s=x "${i}")"
        echo "${PWD}/${i} | ${dimensions}" | sort -h
    done
done

Upvotes: 0

cdlvcdlv
cdlvcdlv

Reputation: 1018

I think the answers are overkilling. Just an ffprobe command, no files written, no console messsages:

for /f "delims=" %%a in ('ffprobe -hide_banner -show_streams %1 2^>nul ^| findstr "^width= ^height="') do set "mypicture_%%a"

And you end with the nice environment variables mypicture_width and mypicture_height. You can check it with:

C:\>set mypicture_
mypicture_height=480
mypicture_width=640

If the size of your picture is 640x480, of course.

Upvotes: 1

Na Nonthasen
Na Nonthasen

Reputation: 182

Just escape the = character ^ and split to retrieve w and h (there maybe a way to retrieve them at once but I don't know).

@echo off

for /f "tokens=5 delims==_" %%i in ('ffprobe -v error -of flat^=s^=_ -select_streams v:0 -show_entries stream^=width %1') do set W=%%i
echo %W%

for /f "tokens=5 delims==_" %%i in ('ffprobe -v error -of flat^=s^=_ -select_streams v:0 -show_entries stream^=height %1') do set H=%%i
echo %H%

Upvotes: 1

mrzed
mrzed

Reputation: 26

I have managed to adjust you script and it's working, you can try this way :

@echo off
ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=width %1 >> width.txt
ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height %1 >> height.txt
FOR /f "tokens=5 delims==_" %%i in (width.txt) do @set width=%%i
FOR /f "tokens=5 delims==_" %%i in (height.txt) do @set height=%%i
echo width=%width%
echo height=%height%
del width.txt && del height.txt
pause

Upvotes: 1

Related Questions