I have to check the video is landscape or portrait before rotate using ffmpge . please help me.
Asked
Active
Viewed 4,713 times
3 Answers
3
Getting video dimension from ffmpeg -i
- Get info.
- Extract dimensions.
- If x < y, then portrait. Else landscape.
eg:
$output = shell_exec("ffmpeg -i $myvideo");
$out_arr = explode("\n", $output);
foreach($out_arr as $line) {
if( preg_match('/^Stream.*Video:/', trim($line)) ) {
// match line: Stream #0.0(und): Video: h264 (High), yuv420p, 640x360 [PAR 1:1 DAR 16:9], 597 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc
$line_arr = explode(',', $line);
// get field: 640x360 [PAR 1:1 DAR 16:9]
$target_arr = explode(' ', $line_arr[2]);
// get parts: 640x360
$dims = explode('x', $target_arr[0]);
$res_x = $dims[0];
$res_y = $dims[1];
}
}
if( !( isset($res_x) && isset($res_y) ) ) {
die('Could not get dimensions');
} else {
$orientation = ($res_x < $res_y) ? 'Portrait' : 'Landscape';
printf('Resolution: %s x %s\nOrientation: %s\n', $res_x, $res_y, $oreintation);
I don't know why you'd want to rotate the video from how it was shot, though. You're going to end up with sideways videos after rotating more likely than not.
-
@thanigai if your app is not making use of the sensors in the phone to determine the orientation when the video is shot, then it will *always* be coming in in whatever default orientation iPhone uses. In this case videos will always need to be rotated by a human. – Sammitch Mar 09 '13 at 18:54
-
in iphone its fine. we take and uploaded a video in portrait mode thorugh iphone but when we see through website(not iphone) it plays sideways – – skythanigai Mar 19 '13 at 17:50
-
it is possible to identify the videos either portrait or landscape because the portrait video only i have to rotate. – skythanigai Mar 19 '13 at 18:21
-
because the portrait videos plays sideways – skythanigai Mar 19 '13 at 18:37
3
using ffmpeg we cant check the video is taken landscape mode or portrite mode in iphone.
we need to install mediainfo or exiftool
if we install exiftool use the below commands
exec('exiftool path/to/filename/ | grep Rotation');
from this we can get the rotation of the videos
if rotation is 90° the videos taken in portraite mode in iphone
if rotation is 0° the videos taken in landscape mode in iphone

skythanigai
- 93
- 1
- 9
-
1This works exactly like: ffprobe -v error -select_streams v:0 -show_entries stream_tags=rotate -of csv=p=0 myfile.mp4 – John Smith Jun 06 '22 at 23:56
-
0
Sometimes we are getting 2 types of output "640x268 [SAR 1:1 DAR 160:67]" and "640x268"
$output = shell_exec("FFmpeg -i ".$localVideoPath." -vstats 2>&1");
$out_arr = explode("\n", $output);
foreach($out_arr as $line) {
if( preg_match('/^Stream.*Video:/', trim($line)) ) {
$line_arr = explode(',', $line);
if (str_contains($line_arr[2], 'x')) {
//540x960
$target_arr = explode(' ', $line_arr[2]);
$dims = explode('x', $target_arr[1]);
}else{
//640x268 [SAR 1:1 DAR 160:67]
$target_arr = explode(' ', $line_arr[3]);
$dims = explode('x', $target_arr[1]);
}
$res_x = $dims[0];
$res_y = $dims[1];
}
}
if(!(isset($res_x) && isset($res_y))){
//die('Could not get dimensions');
} else {
$orientation = ($res_x < $res_y) ? 'Portrait' : 'Landscape';
}

ajmirani
- 366
- 3
- 8