简体   繁体   English

FFMPEG和PHP将视频转换为mp4和快照

[英]FFMPEG with PHP convert video to mp4 and snapshots

I have recently started working on a website where users can upload videos to, and I am a beginner with ffmpeg, however I have been searching and searching for how to do these things I want to do, however most of them aren't working for me. 我最近开始在一个用户可以向其上传视频的网站上工作,并且是ffmpeg的初学者,但是我一直在搜索和搜索如何做这些我想做的事情,但是其中大多数都不起作用我。

I would like to convert any video type to .mp4 and keep the quality as good as possible. 我想将任何视频类型转换为.mp4并保持质量尽可能好。 Right now my code is only working for .mov and .mp4 uploads, however .avi aren't converting (it doesn't even make a new file for those). 现在,我的代码仅适用于.mov.mp4上传,但是.avi并未转换(它甚至没有为这些文件创建新文件)。

I would also like to take high quality screenshots from the video as well, but the screenshots I am getting now seem to be really low quality, so I don't know what's up with that. 我也想从视频中获取高质量的屏幕截图,但是现在获取的屏幕截图似乎质量很低,所以我不知道这是怎么回事。

The code I'm using now is this: 我现在使用的代码是这样的:

$ffmpeg = "/Users/USERNAME/ffmpeg/ffmpeg";
$src    = "/Applications/MAMP/htdocs/lounge/videos/".$filename."_original.".$ext;
$img    = "/Applications/MAMP/htdocs/lounge/videos/thumbnails/".$filename."_1.jpg";
$img2   = "/Applications/MAMP/htdocs/lounge/videos/thumbnails/".$filename."_2.jpg";
$img3   = "/Applications/MAMP/htdocs/lounge/videos/thumbnails/".$filename."_3.jpg";
$video  = "/Applications/MAMP/htdocs/lounge/videos/".$filename.".mp4";

$src = preg_replace("/ /", "\ ", $src);
$img = preg_replace("/ /", "\ ", $img);

$imgOutput  = shell_exec("$ffmpeg -ss 1 -i $src -s 960x540 -f image2 -vframes 1 $img");
$imgOutput2 = shell_exec("$ffmpeg -ss 15 -i $src -s 960x540 -f image2 -vframes 1 $img2");
$imgOutput3 = shell_exec("$ffmpeg -ss 30 -i $src -s 960x540 -f image2 -vframes 1 $img3");

$videoOuput = shell_exec("$ffmpeg -y -i $src 2>&1 -s vga $video");

How could I go about outputting 720p or 1080p videos? 如何输出720p或1080p视频?

FFMpeg is a very powerful solution, however you'll need to get used to its very numerous options to get what you want. FFMpeg是一个非常强大的解决方案,但是您需要习惯于众多选择才能获得所需的内容。

First of all, what you need is to identify what software/hardware will play your videos. 首先,您需要确定要播放视频的软件/硬件。

I will assume here that the videos are going to be played on a website probably using a flash player, or an HTML5 player (JWPlayer is both, for instance). 我将在这里假设视频将可能在网站上使用Flash播放器或HTML5播放器(例如JWPlayer两者)播放。

Then you need to find the best video and audio format for the quality you need. 然后,您需要找到最佳质量的视频和音频格式。 Please be aware that .mp4 is just a container, that can contain various video and audio formats. 请注意, .mp4只是一个容器,可以包含各种视频和音频格式。 FFMpeg is smart enough to use by default AVC (H.264) for video, and AAC for audio, which is good, but probably will not give you perfect results, unless you specify the bitrate or quality you want. FFMpeg足够聪明,可以默认将AVC(H.264)用于视频,将AAC用于音频,这很好,但是除非您指定所需的比特率或质量,否则可能无法获得理想的效果。

Please read this extremely useful link http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide before proceeding further. 在继续之前,请阅读此非常有用的链接http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide

You should probably use ABR or CBR mode if you want to ensure a predictible network throughput, or a CRF mode (18 to 20 will give you great quality) if you are concerned mainly about quality. 如果您想确保可预测的网络吞吐量,则可能应该使用ABR或CBR模式,或者如果您主要关注质量,则可以使用CRF模式(18到20将为您提供高质量)。

Resizing video is a complex topic by itself. 调整视频大小本身就是一个复杂的话题。 You can find yourself being confronted to videos with a non-square Sample Aspect Ratio, meaning the video is meant to be watched in a size different from the actual width or height, or simply videos with a 4:3 ratio that you will try to resize to a 16:9 ratio, causing deformations. 您会发现自己遇到的样本宽高比不是方形的视频,这意味着观看视频的尺寸应与实际的宽度或高度不同,或者只是尝试以4:3的比例观看的视频调整为16:9的尺寸,导致变形。

Using the -s option actually inserts a scale filter which resize video frames using by default a bi-linear filtering algorithm. 使用-s选项实际上会插入一个scale过滤器,该过滤器默认情况下使用双线性过滤算法来调整视频帧的大小。 This is usually good enough for your needs, but it is sometimes more interesting to use a filter explicitly, if you are going to do other processing steps on the video. 通常,这足以满足您的需求,但是如果您要对视频执行其他处理步骤,则显式地使用过滤器有时会更有趣。 For example: 例如:

-vf "scale=-1:720"

This forces a height of 720 pixels, but ensures that the output ratio is the same as the input ratio, so the video will not be deformed. 这会强制720像素的高度,但要确保输出比例与输入比例相同,因此视频不会变形。

Be aware that actually making a video bigger will not improve its quality anyways, will increase processing time, as well as the file size. 请注意,实际上将视频放大是无法改善质量,反而会增加处理时间以及文件大小。

Finally, there is an issue that MUST be taken care of after an MP4 encoding for files that will be played via HTTP. 最后,存在一个问题,必须对将通过HTTP播放的文件进行MP4编码后,必须加以处理。 MP4 files contain an atom called MOOV which is a block of data describing the contents of the file. MP4文件包含一个称为MOOV的原子,MOOV是描述文件内容的数据块。 A player software needs to read this block in order to be able to start playback, which is not an issue locally if the file is found on disk, but blocks file playing when downloaded from HTTP if it is found at the end of the file, because the WHOLE file must be downloaded before playback can start. 播放器软件需要读取此块才能开始播放,如果在磁盘上找到该文件,则不是本地问题;但是,如果在文件末尾找到从HTTP下载的文件,则该块会阻止播放文件,因为必须先下载WHOLE文件,然后才能开始播放。 Guess what, FFMpeg puts the MOOV atom at the end of the file, therefore it is not suited for HTTP progressive playback by default. 猜猜是什么,FFMpeg将MOOV原子放在文件的末尾,因此默认情况下它不适合HTTP渐进播放。

You must use a tool like qt-faststart or MP4Box to modify the file to place the MOOV atom at the beginning. 您必须使用qt-faststart或MP4Box之类的工具来修改文件,以将MOOV原子放在开头。

Have you tried -preset veryslow -qp 0 ? 您是否尝试过-preset veryslow -qp 0 Read in SirDarius' suggested link as a preset for lossless H.264 best compression. 阅读SirDarius的建议链接,将其作为预设以实现无损H.264最佳压缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM