How to convert uploaded video and get a screenshot from this file?

寵の児 提交于 2019-11-30 05:26:37

Answering author's question:

Does ffmpeg requires to be installed server side or just exe is enough?

ffmpeg.exe will be enough, no installation is required.

The code below gets a screenshot on captureTime on video specified by videoFilename variable, and saves it to the imageFilename path.

Process ffmpeg = new Process();
ffmpeg.EnableRaisingEvents = true;
ffmpeg.StartInfo = new ProcessStartInfo
{
    FileName = this.ffmpegPath,
    Arguments = string.Format(
        "-i \"{0}\" -an -y -s 320x240 -ss {1} -vframes 1 -f image2 \"{2}\"",
        this.videoFilename,
        DateTime.MinValue.Add(this.captureTime).ToString("HH:mm:ss:ff", CultureInfo.InvariantCulture),
        this.imageFilename
    ),
    WorkingDirectory = this.workingDirectory,
    UseShellExecute = false,
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    WindowStyle = ProcessWindowStyle.Hidden
};

ffmpeg.Start();
ffmpeg.WaitForExit(this.timeout);

I've used ffmpeg, but I found it easier to just use the pre-compiled .exe version. So in the backend, I would just launch ffmpeg.exe with the required command-line arguments to do the conversion, let it run and when it was finished the completed file was all ready to go.

A long, long time ago in my PHP4 days I used the following method, calling ffmpeg on the shell and creating a screenshot.

/**
 * Create a snapshot of a videofile and save it in jpeg format
 */
function snapshot($sourcefile,$destfile,$width=184,$height=138,$time=1){
    $width=floor(($width)/2)*2;
    $height=floor(($height)/2)*2;
    exec("ffmpeg -i {$sourcefile} -s {$width}x{$height} -t 0.0001 -ss {$time} -f mjpeg {$destfile}");
}

It takes a supported video file as $sourcefile. The desired file location for the screenshot can be given by the $destfile parameter. Off course make sure that the given location is writeable for the executing user.

Hopefully this is also usable for anyone else who's looking for the right syntax.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!