如何处理多个视频上传过程中视频编码的排队问题?
我正在开发一个视频流网站,用户可以将视频上传到该网站(使用 uploadify jquery 插件一次上传多个视频)。
现在,我面临着将视频编码为 FLV 以便在线播放的问题。
视频编码过程应该何时进行?它是否应该在上传完成后立即进行(即重定向用户到上传成功页面,然后使用 ffmpeg 的 exec 命令在后台开始编码?)但是,使用这种方法,我如何确定编码是否已成功完成?如果用户上传损坏的视频并且 ffmpeg 无法对其进行编码怎么办?我如何在 PHP 中处理这个问题?
由于多个用户可以同时上传视频,我如何对视频编码进行排队? FFMpeg 有自己的编码队列吗?
我还在另一个相关的 SO 线程中阅读了有关 gearman 和消息队列选项的信息,例如 redis 和 AMQP。这些是潜在的解决方案之一吗?
如果有人能回答我的问题,我将不胜感激。
I am working on developing a video streaming site where users can upload videos to the site (multiple videos at once using the uploadify jquery plugin).
Now, I am faced with the question of encoding the videos to FLV for streaming them online.
When should the video encoding process take place ? Should it take place immediately after uploads have finished (i.e redirect the user to upload success page, and then start encoding in the background using exec command for ffmpeg ?) However, using this approach, how do i determine if the encoding has finished successfully ? What if users upload a corrupt video and ffmpeg fails to encode it ? How do i handle this in PHP ?
How do i queue encoding of videos since multiple users can upload videos at the same ? Does FFMpeg has its own encoding queue ?
I also read about gearman and message queueing options such as redis and AMQP in another related SO thread. Are these one of the potential solutions ?
I would really appreciate if someone could give answers to my questions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用名为 gearman 的软件。它是一个作业服务器,您可以使用 PHP 调用函数。您可以将进程转移到后台,它会自动处理排队。许多进程也可以并行运行。
我已经使用过它,安装和操作非常简单。
对于您的用例,
该过程完成后,您可以调用另一个函数向用户发送电子邮件、推文等,表明编码已完成。
You should use a software called gearman. It is a job server and you can call functions using PHP. You can transfer the process to background and it automatically handles queuing. Many processes can be run parallel also.
I've used it and it is very easy to install and operate.
For your use case,
After the process is done, you can call another function to send an email, tweet etc to the user that the encoding is complete.
由于编码可能需要一些时间,您可能需要将输入文件添加到文件夹中并在数据库中添加条目。然后,您有一个持续运行或每 x 分钟运行的脚本,将待处理的视频从数据库转换为 FLV 格式。
要对它们进行排队,您需要创建一个自定义脚本来为每个文件重新运行 FFMpeg。
Since encoding may take some time you may want to add the input files in a folder and add an entry in a database. Then you have a script that runs either constantly or each x minutes that convert the pending videos from the database to FLV format.
To queue them you will need to make a custom script that re-run FFMpeg for each files.
您可以在服务器上使用 cron 作业,或者使用 beanstalkd 之类的东西(或该问题的其他答案中提到的 gearman)。
You could use a cron job on the server, or use something like beanstalkd (or gearman as mentioned in other answers to this question).
FFMpeg 只是一个命令行实用程序。它没有任何队列,您需要构建自己的排队系统来异步执行工作。
对于 PHP 队列,我使用 pheanstalk PHP 客户端在 beanstalkd 服务器上获得了很好的体验。然后,您的上传脚本应将项目插入队列中进行编码,并向用户返回响应,表示视频将很快得到处理。您的工作人员应该从队列中获取项目并对其运行 FFMpeg。您可以读取 FFMpeg 状态代码来判断编码是否成功完成。
FFMpeg is just a command line utility. It doesn't have any queue and you will need to build your own queuing system to perform the work asynchronously.
For PHP queues, I had a good experience with the beanstalkd server, using the pheanstalk PHP client. Your upload script should then insert items to the queue for encoding, and return a response to the user saying the video will be processed shortly. Your workers should fetch items from the queue and run FFMpeg on them. You can read the FFMpeg status code to figure whether the encoding was successfully completed.