I'm just learning how to use ffmpeg a few hours ago to generate video thumbnails.
These are some results:
I'd used the same size (width - height) to Youtube's. Each image contains max 25 thumbnails (5x5) with the size 160x90.
Everything looks good until:
public async Task GetVideoThumbnailsAsync(string videoPath, string videoId)
{
byte thumbnailWidth = 160;
byte thumbnailHeight = 90;
string fps = "1/2";
videoPath = Path.Combine(_environment.WebRootPath, videoPath);
string videoThumbnailsPath = Path.Combine(_environment.WebRootPath, $"assets/images/video_thumbnails/{videoId}");
string outputImagePath = Path.Combine(videoThumbnailsPath, "item_%d.jpg");
Directory.CreateDirectory(videoThumbnailsPath);
using (var ffmpeg = new Process())
{
ffmpeg.StartInfo.Arguments = $" -i {videoPath} -vf fps={fps} -s {thumbnailWidth}x{thumbnailHeight} {outputImagePath}";
ffmpeg.StartInfo.FileName = Path.Combine(_environment.ContentRootPath, "FFmpeg/ffmpeg.exe");
ffmpeg.Start();
}
await Task.Delay(3000);
await GenerateThumbnailsAsync(videoThumbnailsPath, videoId);
}
I'm getting a trouble with the line:
await Task.Delay(3000);
When I learn the way to use ffmpeg, they didn't mention about it. After some hours failed, I notice that:
An mp4 video (1 min 31 sec - 1.93Mb) requires some delay time ~1000ms. And other, an mp4 video (1 min 49 sec - 7.25Mb) requires some delay time ~3000ms.
If I don't use Task.Delay
and try to get all the files immediately, it would return 0 (there was no file in the directory).
Plus, each file which has a difference length to the others requires a difference delay time. I don't know how to calculate it.
And my question is: How to check when the task has completed?
P/s: I don't mean to relate to javascript, but in js, there is something called Promise
:
var promise = new Promise(function (done) {
var todo = function () {
done();
};
todo();
});
promise.then(function () {
console.log('DONE...');
});
I want to edit the code like that.
Thank you!