使用ffmpeg转换文件时PHP内部服务器错误500

发布于 2024-10-11 16:33:16 字数 1093 浏览 4 评论 0原文

我有一个 PHP 脚本,在我的服务器中通过 cron 作业调用它来运行上传的视频转换。它对于某些视频工作正常,但当视频稍大一点(例如 21MB)时,我会收到 500 Internal Server Error 并且没有其他输出。

我认为这个问题可能是由于超时造成的,所以我添加了 set_time_limit(9000)ini_set('max_execution_time', 9000) 来防止这种情况发生,但是这并不能解决任何问题。

我使用以下命令执行 ffmpeg:

$cmdOut = shell_exec ('ffmpeg -y -i [....] 2>&1'); // [....] is the rest of the command, it works fine with other videos, so i assume that it works ok.
echo print_r($cmdOut);

但是没有输出,并且以下行没有被执行,因此在 shell_exec 之后脚本被中止。

查看 apache error_log 我可以看到这一行:

[Wed Jan 12 00:12:46 2011] [error] [client xx.xxx.xxx.xxx] Premature end of script headers: index.php

但没有其他线索。谁能帮助我吗?

出于测试目的,我创建了这个 PHP 脚本:

<?php
set_time_limit(300);
sleep(120);
echo "SLEEP OUT";
?>

当我从 Web 浏览器调用该脚本时,该脚本会导致“500 内部服务器错误”,因此我认为 set_time_limit 不起作用。如果我设置 sleep(30) 它会起作用并返回文本 SLEEP OUT。所以问题是,如何避免执行需要 5 或 10 分钟才能完成的 PHP 脚本时出现超时错误?

注意:服务器是 CentOS,运行 apache 和 php 作为 FastCGI 模块。

I've a PHP script that it's being called with a cron job in my server to run uploaded video conversions. It works fine for some videos, but when the video is a bit larger (21MB for example) I get a 500 Internal Server Error and no other output.

I think that it's possible that this problem was due to timeouts so I've added set_time_limit(9000) and also ini_set('max_execution_time', 9000) to prevent this, but this does not solve anything.

I execute ffmpeg using:

$cmdOut = shell_exec ('ffmpeg -y -i [....] 2>&1'); // [....] is the rest of the command, it works fine with other videos, so i assume that it works ok.
echo print_r($cmdOut);

However there is no output, and the following lines are not being executed, so after the shell_exec the script is aborted.

Looking at the apache error_log I can see this line:

[Wed Jan 12 00:12:46 2011] [error] [client xx.xxx.xxx.xxx] Premature end of script headers: index.php

But there are no other clues. Can anyone help me?

For testing purposes i've created this PHP script:

<?php
set_time_limit(300);
sleep(120);
echo "SLEEP OUT";
?>

This script causes a "500 Internal Server Error" when i call it from my web browser, so i suppose that set_time_limit is not working. If i put sleep(30) it works and it returns the text SLEEP OUT. So the question is, how can i avoid the timeout error to execute a PHP script that is taking 5 or 10 minutes to complete??

NOTE: Server is CentOS running apache and php as FastCGI module.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知你几分 2024-10-18 16:33:16

最后我自己解决了这个问题。我开发了一种解决方法来绕过 php.ini 中的超时限制。我的解决方案是使用 php-cli 命令和计划的 cron 作业来执行 php 脚本。这样我在执行脚本时就没有时间限制并且效果很好。

感谢所有人,特别是 Phoenix 的时间和关于这个问题的想法。

Finally i've solved this on my own. I've developed a workaround to bypass the timeout limitation in php. My solution is to execute the php script using the php-cli command with a scheduled cron job. This way i don't have the time limit when executing my script and it works nicely.

Thanks to all, specially to Phoenix for their time and ideas about this issue.

凯凯我们等你回来 2024-10-18 16:33:16

您真的需要控制台输出吗?我曾经遇到过类似的问题,即使我修改了主php.ini本身以延长执行时间限制,但通过exec执行ffmpeg时它仍然会随机下降。最终不得不 > /dev/null & 来阻止它放弃执行,然后无论抛出什么,它都可以正常工作。

Do you really need the console output for anything? I ran into a similar issue once, even though I modified the primary php.ini itself to extend the execution time limit, it would still drop randomly when doing ffmpeg through exec. Wound up having to > /dev/null & it to stop it from dropping execution, then it worked fine regardless of what was thrown at it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文