Search…

X3 Photo Gallery Support Forums

Search…
 
User avatar
amwpsaa
Experienced
Topic Author
Posts: 53
Joined: 29 Jul 2021, 08:46

ffmpeg generates multiple video thumbnails that cause the server to slow down

10 Dec 2021, 12:53

If multiple videos are uploaded in a directory, thumbnails will be generated at the same time when they are accessed for the first time, which will cause the server with low configuration to crash.

Submitted almost at the same time
Attachments
QQ截图20211211015050(1).jpg
QQ截图20211211015050(1).jpg (75 KiB) Viewed 191 times
 
User avatar
mjau-mjau
X3 Wizard
Posts: 13146
Joined: 30 Sep 2006, 03:37

Re: ffmpeg generates multiple video thumbnails that cause the server to slow down

10 Dec 2021, 20:28

The issue is not clear. What do you mean "crash"? All I see in your screenshot is "warning" with slow output, meaning the PHP will timeout and the script will fail (but this is not "crash"). You can try to increase the PHP max_execution_time (by default 30 seconds, clearly indicated in your screenshot).
https://www.php.net/manual/en/info.conf ... ution-time

When Files app shows a video in the layout, it will forward a thumbnail-resize request to FFmpeg in your command-line. We can't speed up this process.
http://ffmpeg.org/

Creating resized thumbnails with FFmpeg (or any other decoder) IS SLOW, even on fast servers, and this is not related to PHP. Actually, the slowness isn't necessarily related to CPU, but it's just slow because FFmpeg needs to open video tracks and navigate the videos internally to find a frame to save as thumbnail. So when I see your screenshot "executing too slow", this is just because video-thumbnail creation and FFmpeg is slow. There is no way Files app or PHP can speed this up for you beyond your server's capacity.

Luckily, you have Files app will CACHE them, and that means that video-thumbnails will load much faster next visits.
amwpsaa wrote:Submitted almost at the same time
Yes of course. They are requested as soon as the visitors browser is trying to display them, which is often at the same time for several thumbnails. What do you propose?

Unfortunately, video thumbnails is slow and there is no way around this. After they are created, they will get cached.
 
User avatar
amwpsaa
Experienced
Topic Author
Posts: 53
Joined: 29 Jul 2021, 08:46

Re: ffmpeg generates multiple video thumbnails that cause the server to slow down

11 Dec 2021, 06:31

mjau-mjau wrote:
10 Dec 2021, 20:28
What do you propose?
After my testing, 10 videos can cause the low-configuration server to crash directly, because the ffmpeg is processing at the same time and cannot respond, but if it is a single video, the processing speed is very fast. The solution I came up with: 1. Use the queue method to submit processing. 2, use a timer, the interval must be to process a request
 
User avatar
mjau-mjau
X3 Wizard
Posts: 13146
Joined: 30 Sep 2006, 03:37

Re: ffmpeg generates multiple video thumbnails that cause the server to slow down

11 Dec 2021, 07:15

amwpsaa wrote:After my testing, 10 videos can cause the low-configuration server to crash directly,
Can you please tell me exactly how it "crashes directly"? I saw your screenshot, and that is NOT crash, but TIMEOUT. If the server crashes, it means it dies and requires restart. FFmpeg is command-line, and is not part of PHP ... PHP just waits for it to execute. I would like to understand how server crashes from this ... Screenshots or errorlog?
amwpsaa wrote:The solution I came up with: 1. Use the queue method to submit processing. 2, use a timer, the interval must be to process a request
Is this some solution you have implemented? If so, please tell me how ...

There is no way a queue is in total faster than making the requests as the images are loaded in browser.
 
User avatar
amwpsaa
Experienced
Topic Author
Posts: 53
Joined: 29 Jul 2021, 08:46

Re: ffmpeg generates multiple video thumbnails that cause the server to slow down

11 Dec 2021, 13:56

mjau-mjau wrote:Is this some solution you have implemented? If so, please tell me how ...
Oh, sorry, the screenshot server did not crash, just waited for a long time, and there will be a part of the thumbnail size of 0, not displayed properly. Another low-configuration test server crashed outright. I can't think of a plan either, but I'll improve by modifying the fpmpeg command

Code
    // ffmpeg command

    $cmd = escapeshellarg(config::$config['video_ffmpeg_path']) . ' -i ' . escapeshellarg($path) . ' -threads 2 -an -ss 1 -s 480x320 -vf "thumbnail,scale=480:320:force_original_aspect_ratio=increase,crop=480:320" -r 1 -y -f image2 -vframes 1 ' . $cache . ' 2>&1';
 
User avatar
mjau-mjau
X3 Wizard
Posts: 13146
Joined: 30 Sep 2006, 03:37

Re: ffmpeg generates multiple video thumbnails that cause the server to slow down

12 Dec 2021, 00:31

Thanks for the feedback. I already knew that FFmpeg was slow, even on dedicated servers, but PHP cannot speed up this process which runs separately on server as a command-line utility. About implementing "queue method", this is not productive for many reasons: 1. Just because thumbnails process in "queue", does not mean all thumbnails will resize faster. Total resize time should be slower. 2. Queue method would just be a workaround to solve insufficient PHP max-execution-time. 3. Browsers make "parallel" requests, because it's faster, and this is causing the parallel requests to resized videos. 4. What happens when videos get cached? Files app doesn't know that up front, so it would be queueing already-cached images instead of loading them in parallel. 5. It's simply not logical or practical to implement. Files app loads images as they appear on screen as unique image requests. It's not practical to block this behavior and attempt to build a javascript-based queue-system that blocks requests until they load in one after the other.

As noted earlier, as long as the thumbnails get created once, they get cached so they will load much faster after first visit. Essentially, you need server resources that are sufficient to create thumbnails, from your specific videos, without exceeding the PHP max-execution-time.

A workaround solution would be to simply extend the max-execution-time using PHP set_time_limt(). For example, locate the FFmpeg command in Files index.php:
Code
$cmd = escapeshellarg( ...
Increase timeout:
Code
set_time_limit(300); // seconds
$cmd = escapeshellarg( ...
No it won't speed up the process, but it will allow thumbnails to eventually get created without causing timeout. And after they are created once, they are cached of course. If your server is limited and you want video thumbnails, there is no other way around this apart from allowing additional processing time.
amwpsaa wrote:and there will be a part of the thumbnail size of 0, not displayed properly.
I don't think this is related. I have seen FFmpeg create 0kb thumbnails when it encounters video formats that it cannot process for some reason. We cannot detect this through PHP. Besides, if there is a PHP timeout while FFmpeg is processing, the PHP will stop processing without creating the thumbnail.
amwpsaa wrote:
Code
    // ffmpeg command

    $cmd = escapeshellarg(config::$config['video_ffmpeg_path']) . ' -i ' . escapeshellarg($path) . ' -threads 2 -an -ss 1 -s 480x320 -vf "thumbnail,scale=480:320:force_original_aspect_ratio=increase,crop=480:320" -r 1 -y -f image2 -vframes 1 ' . $cache . ' 2>&1';
So you added -threads 2 and removed -t 1? I don't see how "threads" will help much, because the lower the value, the less CPU used, but the slower the process will be. The total CPU required to process video thumbnail requests will remain the same. As for removing "-t 1" yes that can speed up requests, because FFmpeg doesn't have to fast-forward to 1 second into the video. However, my tests showed that without this value, thumbnails were often created from the very beginning of videos, often causing a black thumbnail.

My recommendation for you, is to try the set_time_limt(300) option mentioned above. Yes it will still be slow, but they will eventually get created and cached. If you want to use FFmpeg to create videos from thumbnails, you need a server that has sufficient resources.
 
User avatar
mjau-mjau
X3 Wizard
Posts: 13146
Joined: 30 Sep 2006, 03:37

Re: ffmpeg generates multiple video thumbnails that cause the server to slow down  Topic is solved

10 Jan 2022, 02:38

@amwpsaa can you please try the following fix in index.php? It generates video thumbnails much faster, and should have better support for various characters. Locate existing $cmd around line 609:
Code
$cmd = escapeshellarg(...
Replace entire line with new code:
Code
$cmd = escapeshellarg(config::$config['video_ffmpeg_path']) . ' -ss 3 -t 1 -hide_banner -i "' . str_replace('"', '\"', $path) . '" -vframes:v 1 -an -vf "thumbnail,scale=480:320:force_original_aspect_ratio=increase,crop=480:320" -r 1 -y -f mjpeg "' . $cache . '" 2>&1';
Thanks for trying, and please let me know.
 
User avatar
amwpsaa
Experienced
Topic Author
Posts: 53
Joined: 29 Jul 2021, 08:46

Re: ffmpeg generates multiple video thumbnails that cause the server to slow down

10 Jan 2022, 03:25

mjau-mjau wrote:
10 Jan 2022, 02:38
Okay, I'll test in a variety of server environments
 
User avatar
amwpsaa
Experienced
Topic Author
Posts: 53
Joined: 29 Jul 2021, 08:46

Re: ffmpeg generates multiple video thumbnails that cause the server to slow down

11 Jan 2022, 10:43

mjau-mjau wrote: Thanks for trying, and please let me know.
I tested it on a server with 1 core and 1G memory. After modification, the processing speed is very fast. In the past, 20 videos can slow down the server for nearly 1 minute. Now it only takes 5 seconds to complete. This is very useful, thanks for the edit.