【问题标题】:how to check if video is landscape or portrait using ffmpeg php?如何使用ffmpeg php检查视频是横向还是纵向?
【发布时间】:2022-02-10 14:48:59
【问题描述】:

在使用 ffmpge 旋转之前,我必须检查视频是横向还是纵向。 请帮帮我。

【问题讨论】:

    标签: php ubuntu ffmpeg


    【解决方案1】:

    Getting video dimension from ffmpeg -i

    1. 获取信息。
    2. 提取尺寸。
    3. 如果 x

    例如:

    $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);
    

    不过,我不知道您为什么要旋转视频的拍摄方式。旋转后,您最终会看到横向视频。

    【讨论】:

    • @thanigai 如果您的应用没有使用手机中的传感器来确定拍摄视频时的方向,那么它将始终以任何默认值进入iPhone 使用的方向。在这种情况下,视频总是需要人工旋转。
    • 在 iphone 中很好。我们通过 iphone 以纵向模式拍摄并上传了一个视频,但是当我们通过网站(不是 iphone)看到它时,它会横向播放——
    • 可以识别纵向或横向视频,因为只有纵向视频我必须旋转。
    • 因为纵向视频是横向播放的
    【解决方案2】:

    使用 ffmpeg 我们无法检查视频是在 iphone 中拍摄的横向模式还是纵向模式。

    我们需要安装 mediainfo 或 exiftool

    如果我们安装 exiftool,请使用以下命令

    exec('exiftool path/to/filename/ | grep Rotation');

    由此我们可以得到视频的旋转

    如果旋转为 90°,则在 iphone 中以纵向模式拍摄的视频

    如果旋转为 0°,则在 iphone 中以横向模式拍摄的视频

    【讨论】:

      【解决方案3】:

      有时我们会得到两种类型的输出“640x268 [SAR 1:1 DAR 160:67]”和“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';
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        • 2016-12-19
        相关资源
        最近更新 更多