如何使用 PHP 和 ffmpeg 在后台转换视频

时间:2023-04-09
本文介绍了如何使用 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.

我怎样才能做到这一点?

How can I accomplish this?

推荐答案

以下是如何使用 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)的数据库表queue.file_path 包含要处理的上传视频的位置,处理前结果为null,然后根据成功情况为true/false,以及如果失败 error 包含任何消息.如果合适,用户表的主键也可以保存在这里.
  • 每分钟运行一个 Cron 程序来检查队列中是否有任何未处理的项目.
  • 如果有项目在等待,请遍历其中的一些项目并运行您的视频转换代码.您可能希望将其限制为一次处理不超过五个项目,并且任何更多排队的项目都必须等待新的 cron 运行.
  • 在您的 cron 脚本开始时,如果旧副本已经在运行,您需要提前退出.您可以使用 ps aux | 的输出.如果您在类似 *nix 的操作系统中运行,grep (scriptname) 可以提供帮助.
  • 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:

  • 通过创建新的数据库行请求视频转换
  • 重定向到说明正在创建视频的页面
  • 使用网络重定向、AJAX 或网络套接字定期重新检查转换状态.

这种方法对于无法安装自己的队列处理器的共享主机非常有用.但是,如果您使用的是 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 在后台转换视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何使用 ffmpeg 转换视频 下一篇:PHP 和 FFMPEG - 执行智能视频转换

相关文章