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

4

1

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

Corticate answered 29/6, 2016 at 12:50 Comment(0)
H
1

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
Hhd answered 14/10, 2016 at 23:48 Comment(2)
A small explanation of what you added and why would be helpful.Hemia
This way, you will not get the error message : Argument '_' provided as input filename, but 's' was already specified.Hhd
B
1

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%
Bandy answered 3/2, 2017 at 17:39 Comment(0)
D
1

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.

Doctor answered 15/7, 2018 at 22:17 Comment(0)
J
0

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
Jones answered 7/10, 2023 at 2:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.