PHP和FFMPEG - 执行智能视频转换 [英] PHP and FFMPEG - Performing intelligent video conversion

查看:104
本文介绍了PHP和FFMPEG - 执行智能视频转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的艰巨任务来执行。我认为这很简单,但是我的所有努力都没有结果。



我将视频上传到各种格式的php脚本(.avi,.mpg, .wmv,.mov等)到一个.flv格式。转换是非常好的,但我遇到的麻烦就是视频的分辨率。



这是我正在运行的命令(带有PHP vars):



ffmpeg -i $ original -ab 96k -b 700k -ar 44100 -s 640x480 -acodec mp3 $转换



$ original和$ converted都包含这些文件的完整路径。我的问题是,即使源较小,这总是转换为640x480(像我告诉它)。显然,当下载视频时,这是浪费磁盘空间和带宽。此外,这并不考虑输入视频的4:3以外的任何宽高比,如果我上传16:9视频,导致压缩转换。



有三件我需要做的事情:


  1. 确定原始视频的宽高比。


  2. 如果原稿的尺寸较大或与宽度相关的纵横比为4:3,则转换为640x480 /原来的高度(以较接近640x480为准)

我已经运行 ffmpeg -i ,但我看不到一致的格式或位置来找到原始的分辨率。一旦我能够弄清楚,我知道我可以做数学找出正确的大小,并指定填充以使用-padttop,-padbottom等来修正宽高比。

解决方案

这适用于我:

  $ data =' ffmpeg输出'; 
$ matches = array();

if(!preg_match('/ Stream#(?:[0-9\。] +)(?:。*)\:视频:(?P< videocodec& (?P< width> [0-9] *)x(?P< height> [0-9] *)/',$ data,$ matches)
preg_match('/无法找到编解码器参数\\ \\(视频:(?P< videocodec> *)(?P< width> [0-9] *)x(?P< height> [0-9] *)\)/',$ data,$匹配)

这可能不会始终工作,但它的工作大部分时间,这在我的情况下是好的:)


I have an oddly difficult task to perform. I thought it would be easy, but all my efforts have been fruitless.

I'm converting videos uploaded to a php script from various formats (.avi, .mpg, .wmv, .mov, etc.) to a single .flv format. The conversion is working great but what I'm having trouble with is the resolution of the videos.

This is the command I'm currently running (with PHP vars):

ffmpeg -i $original -ab 96k -b 700k -ar 44100 -s 640x480 -acodec mp3 $converted

Both $original and $converted contain the full paths to those files. My problem is that this always converts to 640x480 (like I'm telling it to) even when the source is smaller. Obviously, this is a waste of disk space and bandwidth when the video is downloaded. Also, this doesn't account for input videos being in any aspect ratio other than 4:3, resulting in a "squished" conversion if I upload a 16:9 video.

There are 3 things I need to do:

  1. Determine the aspect ratio of the original video.
  2. If not 4:3, pad top and bottom with black bars.
  3. Convert to 640x480 if either dimension of the original is larger or a 4:3 aspect ratio relating to the width/height of the original (whichever is closer to 640x480).

I've run ffmpeg -i on a few videos, but I don't see a consistent format or location to find the original's resolution from. Once I'm able to figure that out, I know I can "do the math" to figure out the right size and specify padding to fix the aspect ratio with -padttop, -padbottom, etc.

解决方案

This works for me:

$data = 'ffmpeg output';
$matches = array();

if (!preg_match('/Stream #(?:[0-9\.]+)(?:.*)\: Video: (?P<videocodec>.*) (?P<width>[0-9]*)x(?P<height>[0-9]*)/',$data,$matches)
   preg_match('/Could not find codec parameters \(Video: (?P<videocodec>.*) (?P<width>[0-9]*)x(?P<height>[0-9]*)\)/',$data,$matches)

This might not always work, but it works most of the times, which was good enough in my case :)

这篇关于PHP和FFMPEG - 执行智能视频转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆