ffmpeg修复视频方向

时间:2014-03-17 09:16:31

标签: php ffmpeg orientation

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

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

ffmpeg是否为此提供了修复,或者我是否必须采取艰难的方式(Read rotation,旋转,设置元数据)

1 个答案:

答案 0 :(得分:7)

我确实走得很厉害:

$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" ' . $output_file_full . ".rot2.mp4 2>&1") . "\n";
        echo shell_exec($ffmpeg . ' -i ' . $output_file_full . '.rot2.mp4 -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 == "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";
    }
}

我使用了一些丑陋的临时文件。对不起。