2011-03-31 77 views
1

我写了下面的包装为FFMPEG: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; 
} 

正如你可以看到我用我输出的原始输入音频和视频比特率,即使输入视频裁剪或调整,这在HD空间方面似乎相当低效。

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

在此先感谢!

回答

3

一般而言,您不应该指定比特率。它仅适用于流式传输,在这种情况下,您还需要尊重VBV(指定随时间变化的最大比特率以及平均比特率)。

使用x264 crf 23 - 其默认的质量恒定模式 - 并很高兴。在ffmpeg的情况下,这是类似的:

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

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

+0

这是我第一次尝试,但质量是废话。根据FFMPEG文档:** - b比特率:以bit/s(默认= 200 kb/s)**设置视频比特率。至于音频,我将比特率限制为最大128kb。另外,因为我对这个有点新鲜,“crf 23”代表什么? – 2011-04-04 00:31:40

+0

固定费率因子。 – 2011-04-04 00:34:47

+0

Humm,我想我已经在http:// rob中找到它了。opendot.cl/index.php/useful-stuff/ffmpeg-x264-encoding-guide/:恒定速率因子。你知道它究竟做了什么吗?我可以与其他视频编解码器一起使用吗? 23,那么有一个规模? – 2011-04-04 00:35:47

0

你想寻找Shannon-Entropy -log(P)/ log(2)。这是任何信息可以想到的最小位数。但我不确定它对你有用。

+0

以前从未听说过香农熵,我现在要去查看维基百科页面。 – 2011-03-31 15:58:50

+0

我瞥了一眼它,但我不明白我可以如何将这个问题应用于我的问题,我可以使用的变量...请详细说明一下这个问题吗? – 2011-03-31 16:19:18

+0

基本上我的想法是统计数据中的每个符号,然后用Shannon-Entropy公式对其进行求和,以获得数据压缩的最小位数。这是一个理论上的限制,它取决于你的数据和你的算法。我相信它只是用于文本压缩。但我确信我错了,因为我回答了一个类似的问题,在http://stackoverflow.com/questions/5450489/how-does-winrar-perform-a-compression-ratio-check/5451331#5451331之前。用户shelwien是一名压缩专业人士。 – Bytemain 2011-03-31 16:25:52

0

我结束了使用-sameq flag,我读了一些地方,这不会转化为相同的质量,但现在这比强制原始比特率更好。

无论如何,我遇到过this Bash script,这表明我的想法是正确的,我仍然不知道如何计算输出比特率,而没有输出大小作为约束。如果有人知道,请分享!