【问题标题】:ffmpeg fix video orientationffmpeg 修复视频方向
【发布时间】:2014-03-17 09:16:31
【问题描述】:

视频可以包含有关相机方向的元信息。例如,如果您转动设备,iPhone 和其他手机就会设置此标志。问题是当一些播放器读取此信息并相应地旋转视频时,其他播放器没有。

要解决此问题,必须旋转视频并正确设置元信息。

ffmpeg 是否为此提供了解决方案,还是我必须努力解决(Read rotation,旋转,设置元数据)

【问题讨论】:

    标签: php ffmpeg orientation


    【解决方案1】:

    我确实走得很辛苦:

    $ffmpeg == "path/to/ffmpeg";
    $output_file_full = "file/after/normal/conversion";
    
    // get rotation of the video
    ob_start();
    passthru($ffmpeg . " -i " . $output_file_full . " 2>&1");
    $duration_output = ob_get_contents();
    ob_end_clean();
    
    // rotate?
    if (preg_match('/rotate *: (.*?)\n/', $duration_output, $matches))
    {
        $rotation = $matches[1];
        if ($rotation == "90")
        {
            echo shell_exec($ffmpeg . ' -i ' . $output_file_full . ' -metadata:s:v:0 rotate=0 -vf "transpose=1" ' . $output_file_full . ".rot.mp4 2>&1") . "\n";
            echo shell_exec("mv $output_file_full.rot.mp4 $output_file_full") . "\n";
        }
        else if ($rotation == "180")
        {
            echo shell_exec($ffmpeg . ' -i ' . $output_file_full . ' -metadata:s:v:0 rotate=0 -vf "transpose=1,transpose=1" ' . $output_file_full . ".rot.mp4 2>&1") . "\n";
            echo shell_exec("mv $output_file_full.rot.mp4 $output_file_full") . "\n";
        }
        else if ($rotation == "270")
        {
            echo shell_exec($ffmpeg . ' -i ' . $output_file_full . ' -metadata:s:v:0 rotate=0 -vf "transpose=2" ' . $output_file_full . ".rot.mp4 2>&1") . "\n";
            echo shell_exec("mv $output_file_full.rot.mp4 $output_file_full") . "\n";
        }
    }
    

    我使用了一些难看的临时文件。对此感到抱歉。

    【讨论】:

    • eeeeshhhh 这肯定是 hacky。请考虑以下方法以将元数据获取为 json: ffprobe -of json -show_streams your_video.file 2>/dev/null
    • 我知道我来晚了,但与其旋转两次,不如"transpose=2,transpose=2"
    • @ZioCain 你的意思是 $rotation == "180" 吗?可能是这样的:echo shell_exec($ffmpeg . ' -i ' . $output_file_full . ' -metadata:s:v:0 rotate=0 -vf "transpose=1,transpose=1" ' . $output_file_full . ".rot.mp4 2>&1") . "\n";?
    • @PiTheNumber 是的,这就是我真正的意思!感谢您再次提供完整的工作代码!
    • @PiTheNumber ffmpeg 输出并不意味着机器解析。使用ffprobe instead to get the rotation(它会简化你的代码)。
    猜你喜欢
    • 2011-01-13
    • 2017-08-10
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    相关资源
    最近更新 更多