视频可以包含有关相机方向的元信息。例如,如果您打开设备,iPhone 和其他手机会设置此标志。问题是当一些播放器读取此信息并相应地旋转视频时,其他播放器不会。
要解决此问题,必须旋转视频并正确设置元信息。
ffmpeg 是否为此提供修复,还是我必须走艰难的道路(Read rotation,旋转,设置元数据)
最佳答案
我确实走了艰难的路:
$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";
}
}
我使用了一些丑陋的临时文件。对此感到抱歉。
关于php - ffmpeg 修复视频方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22450453/