gpt4 book ai didi

php - 使用 FFmpeg-PHP 的 RAM 问题

转载 作者:行者123 更新时间:2023-12-04 22:51:19 25 4
gpt4 key购买 nike

我面临一个奇怪的问题。
我正在使用 FFmpeg-PHP 创建一些视频的缩略图,我使用 gdlib 将这些缩略图粘贴到更大的预览图片中。
到目前为止一切正常。
我在一个循环中做所有这些,所以 ffmpeg-php 必须制作 10 到 20 张不同视频的预览图片。现在的问题是,如果我的脚本做了第一个预览图片,它不会释放使用的资源,所以我在第 5 个视频之后内存不足。

现在的问题是:为什么?我销毁并取消了所有资源处理程序,等等......

这是脚本:

 <?php
error_reporting(E_ALL);
$extension = "ffmpeg";
$extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;
$extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;

// load extension
if(!extension_loaded($extension)) {
dl($extension_soname) or die("Can't load extension $extension_fullname\n");
echo "Load FFmpeg-php!\n";
}

class VidPreview
{
private $pathToVid;
private $video;
private $preview;
private $previewX;
private $previewY;

public function __construct($pathToVid, $previewX, $previewY)
{
$this->pathToVid = $pathToVid;
$this->previewX = $previewX;
$this->previewY = $previewY;
$this->video = new ffmpeg_movie($this->pathToVid);

//init gd for main picture
$this->preview = imagecreate($this->previewX, $this->previewY);
imagecolorallocate($this->preview, 250, 250, 200);
imagestring($this->preview, 5, $this->previewX/2, 20, "Preview!", 5);
}

private function makeScreenshot($frame)
{
$ff_frame = $this->video->getFrame($frame);
if(method_exists($ff_frame, 'toGDImage'))
{
$screenshot = $ff_frame->toGDImage();
}
else
{
$screenshot = false;
}
unset($ff_frame);
return $screenshot;
}

public function makePreview($pathToSave, $amountOfShots)
{
//get video-frame-number
$frameNumber = $this->video->getFrameCount();
$frameValue = round(($frameNumber / $amountOfShots))-1;
$frameValueStep = $frameValue;

$dst_x = 20;
$dst_y = 40;
while($frameNumber >= $frameValue)
{
$screenshot = $this->makeScreenshot($frameValue);
if($screenshot != false)
{
if($dst_x >= $this->previewX)
{
$dst_y = $dst_y + 20 + 135;
$dst_x = 20;
}
//copy screen to preview
imagecopyresized($this->preview, $screenshot , $dst_x, $dst_y , 0 , 0 , 240 , 135 , 720 , 406 );//TODO FIX VID VALUES
imagedestroy($screenshot);
unset($screenshot);
//TODO FIX PREVIEW BUG
$dst_x = $dst_x + 240 + 20;
$frameValue = $frameValue + $frameValueStep;
echo '$dst_x: '.$dst_x." ".'$dst_y: '.$dst_y.' '.'$frameNumber, $frameValue: '.$frameValue.','.$frameNumber."\n";
}
else
{
fwrite(STDOUT, "FFmpeg-php messed up! I will just skip this screenshot... \n");
unset($screenshot);
$frameValue = $frameValue + $frameValueStep;
}
}

if(!imagejpeg($this->preview, $pathToSave, 100))
{
imagedestroy($this->preview);
unset($this->preview);
unset($this->video);
return false;
}
imagedestroy($this->preview);
unset($this->preview);
unset($this->video);
return true;
}

function __destruct()
{
unset($this->pathToVid);
unset($this->video);
unset($this->preview);
unset($this->previewX);
unset($this->previewY);
}
}


?>

希望有人可以提供帮助,它看起来有点讨厌,因为我添加了一些额外的 unset 。

这是函数调用:
        if(!file_exists($pathToPic.'/'.$dirName.".jpg"))
{
fwrite(STDOUT, "Making pictures of: ".$file." \n");
$preview = new VidPreview($pathToVid,1000, 700);
$preview->makePreview($pathToPic.'/'.$dirName.".jpg", 16);
unset($preview);
}

此代码段在 for-each-loop 中运行。

PS:我已经在此处发布了该主题: http://ubuntuforums.org/showthread.php?p=10211381在这里:forums.devnetwork.net/viewtopic.php?f=1&t=125566
我希望你不要生我的气,但这很重要:(

最佳答案

谢谢你的提示。我做到了,这就是结果:

[Start] Function: __construct (Before doing anything). Used Memory: 749240
[wmv3 @ 0xaf8e9d0] Extra data: 8 bits left, value: 0
Function: __construct (After making stuff). Used Memory: 2191352
Function: makePreview (before calling makeScreenshot). Used Memory: 2191808
Function: makeScreenshot (start). Used Memory: 2191808
[wmv3 @ 0xaf8e9d0] Extra data: 8 bits left, value: 0
Function: makeScreenshot (end). Used Memory: 3680464
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191808
$dst_x: 280 $dst_y: 40 $frameNumber, $frameValue: 12230,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 540 $dst_y: 40 $frameNumber, $frameValue: 18345,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 800 $dst_y: 40 $frameNumber, $frameValue: 24460,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 1060 $dst_y: 40 $frameNumber, $frameValue: 30575,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 280 $dst_y: 195 $frameNumber, $frameValue: 36690,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 800 $dst_y: 195 $frameNumber, $frameValue: 48920,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 1060 $dst_y: 195 $frameNumber, $frameValue: 55035,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 280 $dst_y: 350 $frameNumber, $frameValue: 61150,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 540 $dst_y: 350 $frameNumber, $frameValue: 67265,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 800 $dst_y: 350 $frameNumber, $frameValue: 73380,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 1060 $dst_y: 350 $frameNumber, $frameValue: 79495,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 280 $dst_y: 505 $frameNumber, $frameValue: 85610,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 540 $dst_y: 505 $frameNumber, $frameValue: 91725,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 800 $dst_y: 505 $frameNumber, $frameValue: 97840,97851
Function: makePreview (before calling makeScreenshot). Used Memory: 2191856
Function: makeScreenshot (start). Used Memory: 2191856
Function: makeScreenshot (end). Used Memory: 3680512
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2191856
$dst_x: 1060 $dst_y: 505 $frameNumber, $frameValue: 103955,97851
[END] Function: makePreview (After unsetting everything). Used Memory: 749728
[REAL-END] Function: __destruct (After unsetting really everything). Used Memory: 748936

其他视频看起来几乎相同。它总是回到:748936。我用过:memory_get_usage();
以下是 memory_get_usage(true) 的结果:
[Start] Function: __construct (Before doing anything). Used Memory: 1835008
[wmv3 @ 0x19c08dc0] Extra data: 8 bits left, value: 0
Function: __construct (After making stuff). Used Memory: 2359296
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
[wmv3 @ 0x19c08dc0] Extra data: 8 bits left, value: 0
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 280 $dst_y: 40 $frameNumber, $frameValue: 13082,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 540 $dst_y: 40 $frameNumber, $frameValue: 19623,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 800 $dst_y: 40 $frameNumber, $frameValue: 26164,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 1060 $dst_y: 40 $frameNumber, $frameValue: 32705,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 280 $dst_y: 195 $frameNumber, $frameValue: 39246,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 540 $dst_y: 195 $frameNumber, $frameValue: 45787,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 800 $dst_y: 195 $frameNumber, $frameValue: 52328,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 1060 $dst_y: 195 $frameNumber, $frameValue: 58869,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 280 $dst_y: 350 $frameNumber, $frameValue: 65410,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 540 $dst_y: 350 $frameNumber, $frameValue: 71951,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 800 $dst_y: 350 $frameNumber, $frameValue: 78492,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 1060 $dst_y: 350 $frameNumber, $frameValue: 85033,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 280 $dst_y: 505 $frameNumber, $frameValue: 91574,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 540 $dst_y: 505 $frameNumber, $frameValue: 98115,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 3932160
Function: makePreview (After unseting $screenshot of makeScreenshot). Used Memory: 2359296
$dst_x: 800 $dst_y: 505 $frameNumber, $frameValue: 104656,104670
Function: makePreview (before calling makeScreenshot). Used Memory: 2359296
Function: makeScreenshot (start). Used Memory: 2359296
Function: makeScreenshot (end). Used Memory: 2359296
FFmpeg-php messed up! I will just skip this screenshot...
FFmpeg-php messed up! I will just skip this screenshot...
Function: makePreview (After unseting $screenshot of makeScreenshot (case of failure)). Used Memory: 2359296
[END] Function: makePreview (After unsetting everything). Used Memory: 1835008
[REAL-END] Function: __destruct (After unsetting really everything). Used Memory: 1835008

所以我想这里一切都很好,因为:
[Start] Function: __construct (Before doing anything). Used Memory: 749240
[REAL-END] Function: __destruct (After unsetting really everything). Used Memory: 748936

因此,在取消设置所有内容后,它几乎是相同的内存使用情况。

但是“ps aux”告诉我:
root     21637 37.0 **37.9** 303952 198744 pts/1   R+   11:48  20:27 php upload.php

它从 1.2 开始,并在每张图片后增加。 (37.9 是内存使用百分比)

到底是怎么回事?

关于php - 使用 FFmpeg-PHP 的 RAM 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4400484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com