I am trying to get a poster frame from a video file, using ffmpeg.
I have been following this tutorial and come up with the following code(which is taken/adapted from the link I gave):
public bool GetVideoThumbnail(string path, string saveThumbnailTo, int seconds)
{
string parameters = string.Format("-i {0} {1} -vcodec mjpeg -ss {2} -vframes 1 -an -f rawvideo", path, saveThumbnailTo, seconds);
if (File.Exists(saveThumbnailTo))
{
return true;
}
else
{
using (Process process = Process.Start(pathToConvertor, parameters))
{
process.WaitForExit();
}
return File.Exists(saveThumbnailTo);
}
}
At the moment this code is successfully creating a file in the correct destination (saveThumbnailTo) only the picture is completely black. I have tried changing the seconds value in the code to ensure that I am not just getting a blank picture from the start of the video. The path refers to where my video is stored, by the way.
I am currently calling the above code like so:
GetVideoThumbnail(videoPath, folderPath + "/poster.jpg", 100)
..and then passing it out to my view to display the picture. I just wonder whether “.jpg” is the extension I should be giving to this file as I am not entirely sure?
Edit: When I run the same command from the command line I get the following errors:
Incompatible pixel format ‘yuv420p’ for codec ‘mjpeg’, auto-selecting
format ‘yuvj420p’
which appears in yellow, and
[image2 @ 02S96AE0] Could not get frame filename number 2 from pattern
‘poster.jpg’ an_interleaved_write_frame(): Invalid argument
which appears in red.
Could anyone help me with getting this working properly as I am completely unfamiliar with the ffmpeg command line and not sure what I am doing wrong. I have tried removing the vcodec parameter and get the same error message.
Try this:
Short explanation:
Try several times with different values for the “seconds” parameter.
Also, make sure the “pathToConvertor” is correct.
This worked for me, with recent build of ffmpeg.exe on a Windows machine.
Let me know how it goes.