PHP 和 FFMPEG - 执行智能视频转换

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

问题描述

我有一项异常艰巨的任务要执行.本以为很容易,结果一切努力都没有结果.

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

我正在将上传到 php 脚本的各种格式(.avi、.mpg、.wmv、.mov 等)的视频转换为单一的 .flv 格式.转换效果很好,但我遇到的问题是视频的分辨率.

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.

这是我当前正在运行的命令(使用 PHP 变量):

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

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

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.

我需要做三件事:

  1. 确定原始视频的纵横比.
  2. 如果不是 4:3,用黑条填充顶部和底部.
  3. 如果原件的尺寸较大或与原件的宽度/高度相关的宽高比为 4:3(以更接近 640x480 的为准),则转换为 640x480.

我已经在一些视频上运行了 ffmpeg -i,但我没有看到一致的格式或位置来查找原始分辨率.一旦我能够弄清楚这一点,我知道我可以做数学"来找出正确的尺寸并指定填充以使用 -padttop、-padbottom 等来固定纵横比.

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.

推荐答案

这对我有用:

$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 - 执行智能视频转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何使用 PHP 和 ffmpeg 在后台转换视频 下一篇:如何在没有 $_SERVER['HTTPS'] 的情况下确定您是否使用 HTTPS

相关文章