php - FFMPEG比特率计算/优化

标签 php optimization video ffmpeg bitrate

我为 FFMPEG 编写了以下包装器:

function Video($input, $crop = null, $scale = null, $output = null, $extra = null)
{
    $input = @new ffmpeg_movie($input);

    if ((is_object($input) === true) && ($input->hasVideo() === true))
    {
        $size = array($input->getFrameWidth(), $input->getFrameHeight());
        $crop = array_values(array_filter(explode('/', $crop), 'is_numeric'));
        $scale = array_values(array_filter(explode('*', $scale), 'is_numeric'));

        if ((is_callable('shell_exec') === true) && (is_executable($ffmpeg = trim(shell_exec('which ffmpeg'))) === true))
        {
            if (count($crop) == 2)
            {
                $crop = array($size[0] / $size[1], $crop[0] / $crop[1]);

                if ($crop[0] > $crop[1])
                {
                    $size[0] = round($size[1] * $crop[1]);
                }

                else if ($crop[0] < $crop[1])
                {
                    $size[1] = round($size[0] / $crop[1]);
                }

                $crop = array($input->getFrameWidth() - $size[0], $input->getFrameHeight() - $size[1]);
            }

            else
            {
                $crop = array(0, 0);
            }

            if (count($scale) >= 1)
            {
                if (empty($scale[0]) === true)
                {
                    $scale[0] = round($scale[1] * $size[0] / $size[1] / 2) * 2;
                }

                else if (empty($scale[1]) === true)
                {
                    $scale[1] = round($scale[0] * $size[1] / $size[0] / 2) * 2;
                }
            }

            else
            {
                $scale = array(round($size[0] / 2) * 2, round($size[1] / 2) * 2);
            }

            $result = array();

            if (array_product($scale) > 0)
            {
                $result[] = sprintf('%s -i %s', escapeshellcmd($ffmpeg), escapeshellarg($input->getFileName()));

                if (array_sum($crop) > 0)
                {
                    if (stripos(shell_exec(escapeshellcmd($ffmpeg) . ' -h | grep crop'), 'removed') !== false)
                    {
                        $result[] = sprintf('-vf "crop=in_w-2*%u:in_h-2*%u"', round($crop[0] / 4) * 2, round($crop[1] / 4) * 2);
                    }

                    else if ($crop[0] > 0)
                    {
                        $result[] = sprintf('-cropleft %u -cropright %u', round($crop[0] / 4) * 2, round($crop[0] / 4) * 2);
                    }

                    else if ($crop[1] > 0)
                    {
                        $result[] = sprintf('-croptop %u -cropbottom %u', round($crop[1] / 4) * 2, round($crop[1] / 4) * 2);
                    }
                }

                if ($input->hasAudio() === true)
                {
                    $result[] = sprintf('-ab %u -ar %u', $input->getAudioBitRate(), $input->getAudioSampleRate());
                }

                $result[] = sprintf('-b %u -r %u -s %s', $input->getBitRate(), min(25, $input->getFrameRate()), implode('x', $scale));

                if (strlen($format = strtolower(ltrim(strrchr($output, '.'), '.'))) > 0)
                {
                    $result[] = sprintf('-f %s %s -y %s', $format, escapeshellcmd($extra), escapeshellarg($output . '.ffmpeg'));

                    if ((strncmp('flv', $format, 3) === 0) && (is_executable($flvtool2 = trim(shell_exec('which flvtool2'))) === true))
                    {
                        $result[] = sprintf('&& %s -U %s %s', escapeshellcmd($flvtool2), escapeshellarg($output . '.ffmpeg'), escapeshellarg($output . '.ffmpeg'));
                    }

                    $result[] = sprintf('&& mv -u %s %s', escapeshellarg($output . '.ffmpeg'), escapeshellarg($output));

                    if ((is_writable(dirname($output)) === true) && (is_resource($stream = popen('(' . implode(' ', $result) . ') 2>&1 &', 'r')) === true))
                    {
                        while (($buffer = fgets($stream)) !== false)
                        {
                            if (strpos($buffer, 'to stop encoding') !== false)
                            {
                                return (is_int(pclose($stream)) === true) ? true : false;
                            }
                        }

                        if (is_file($output . '.ffmpeg') === true)
                        {
                            unlink($output . '.ffmpeg');
                        }

                        pclose($stream);
                    }
                }
            }
        }
    }

    return false;
}

如您所见,我在输出中使用原始输入音频和视频比特率,即使输入视频被裁剪或调整大小,这在高清空间方面似乎效率很低。

我对这些事情知之甚少,但据我了解,比特率与媒体的持续时间、质量和分辨率直接相关,对吧?如果是这样,我如何使用这些值来确定适当的音频和视频比特率以保持输入质量并减小文件大小?

提前致谢!

最佳答案

一般来说,您根本不应该指定比特率。它仅对流式传输有用,在这种情况下,您还需要遵守 VBV(它指定了一段时间内的最大比特率,以及平均比特率)。

使用 x264 crf 23 - 它的默认恒定质量模式 - 并快乐。在 ffmpeg 的情况下,这类似于:

ffmpeg -i <file> -vcodec libx264 -vpre slower -acodec copy <outfile>

至于音频,如果输入经过压缩,最好直接复制。这在某些情况下是不可能的,例如输入是 vorbis 而输出是 .flv 文件。在这种情况下,我会坚持选择音频编码器的默认值。

关于php - FFMPEG比特率计算/优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5502654/

相关文章:

c++ - 从多个线程写入内存区域会导致争用吗?

Java迷宫打破封闭

javascript - 当我在 php 中从数据库中检索数据并显示它时如何隐藏显示表中的密码字段

java - 在什么时候定义变量比调用方法更有效?

php - 如何使用 laravel 将 mysql 表导出到 xml 文件

linux - ffmpeg img to video = 找不到带路径的文件

javascript - HTML5 视频无法流式传输并且需要 90 秒才能加载

html - HTML5 中的直播视频

phpActiveRecord 不正确的日期时间格式

php - 它不会在数据库中更新或更改