如何使用PHP和ffmpeg在后台转换视频 [英] How to convert video in the background using PHP and ffmpeg

查看:233
本文介绍了如何使用PHP和ffmpeg在后台转换视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我允许用户上传视频,然后使用ffmpeg进行转换。视频需要很长时间才能进行转换,这通常会导致错误。我已经完成了我的研究,没有运气,我应该在哪里开始。

I allow users to upload videos and then they get converted using ffmpeg. The video takes a really long time to convert which usually results in an error. I have done my research with no luck as to where I should get started.

基本上我想做的是允许用户上传视频,然后显示一条消息说视频正在处理中,您可以在收到通知后立即通知您。在此期间,我希望视频在幕后转换,并允许用户离开页面,甚至关闭浏览器。我正在使用Windows服务器。

Basically what I want to do is allow the user to upload the video and then display a message that says video is being processed and you will be notified when available. In the meantime I want the video to be converted behind the scenes and allow the user to leave the page or even close the browser. I am using a Windows server.

如何实现?

推荐答案

这是一个如何使用Cron等调度系统进行自己的队列的基本操作:

Here is a basic run-down of how to make your own queue using a scheduling system such as Cron:


  • 创建数据库包含(id,created_at,file_path,id_user,result,error)的表队列 file_path 包含要上传的视频的位置,处理前的结果为 null 然后 true / false 之后取决于成功,如果失败错误包含任何消息。

  • 每分钟,运行一个Cron程序来检查任何未处理的项目的队列。

  • 如果有等待的项目,循环访问其中的几个,并运行您的视频转换代码。您可能希望限制这一点,所以一次只处理五个项目,任何更多排队的项目都必须等待一个新的cron运行。

  • 在开始您的cron脚本,如果旧版本已经在运行,您需要提前退出。您可以使用 ps aux |的输出grep(scriptname)在这里帮助,如果你正在一个类似* nix的操作系统中运行。

  • Create a database table queue containing (id, created_at, file_path, id_user, result, error). The file_path contains the location of the uploaded video to process, the result is null before processing and then true/false afterwards depending on success, and if it failed error contains any messages. The primary key of the user table can be saved here too, if appropriate.
  • Every minute, run a Cron program to check the queue for any unprocessed items.
  • If there are items waiting, loop through a few of them and run your video conversion code. You may wish to limit this so that no more than, say, five items are processed in one go, and any more queued items have to wait for a new cron run.
  • At the start of your cron script you need to exit early if an old copy is already running. You can use the output of ps aux | grep (scriptname) to help here, if you are running in a *nix-like operating system.

在您的Web应用程序中,您需要稍微修改工作流程 - 而不是期望立即处理视频,您需要:

Inside your web application, you need to somewhat modify the workflow - rather than expecting a video to be processed immediately, you need to:


  • 通过创建新的数据库行来请求视频转换

  • 重定向到解释视频创建的页面正在进行

  • 使用Web重定向,AJAX或Web套接字。

这种方法对于无法安装自己的队列处理器的共享主机非常有用。但是,如果您使用VPS或云系统,您可能希望查看Gearman或许多其他排队系统之一 。它们比上述更复杂一些,但具有管理工作队列的更多功能。

This approach is very useful for shared hosting where you cannot install your own queue processors. However, if you are on a VPS or cloud system, you may wish to look into Gearman or one of many other queueing systems. They are a bit more complex than the above, but have more features for managing queues of work.

这篇关于如何使用PHP和ffmpeg在后台转换视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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