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

3

5

I'm building a cms and I want users to be able to upload videos but I'm not familiar with video upload & conversion. Is there an example or has anybody coded a solution like this? I heard about ffmpeg but I don't know how to integrate it with asp.net.

As simple solution I can make my clients upload flv files but then I would still need to get a screenshot from that fvl.

Thanks

Shame answered 21/4, 2010 at 10:55 Comment(0)
B
6

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);
Blotto answered 21/4, 2010 at 12:27 Comment(3)
@Alex: I couldn't get it work with Asp.net on localhost but the command works just fine: C:\inetpub\wwwroot\ffmpeg.exe -i "c:\inetpub\wwwroot\test.flv" -an -y -s 320x240 -ss 00:00:03:00 -vframes 1 -f image2 "c:\inetpub\wwwroot\test.jpg"Shame
@HasanGursoy: what is the problem with ASP.NET? Maybe this is security issue?Blotto
Exactly. There was no error so I couldn't debug. The output directory must have write permission.Shame
S
2

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.

Shive answered 21/4, 2010 at 11:4 Comment(2)
@codeka: Can you post example command? Does ffmpeg requires to be installed server side or just exe is enough? Because if it is enough I'll place it at bin folder.Shame
I downloaded the exe from: ffmpeg.arrozcru.org the command-line is pretty simple and fairly well documented here: ffmpeg.org/ffmpeg-doc.htmlShive
P
0

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.

Paranoia answered 16/8, 2011 at 16:47 Comment(1)
Do you see php tag anywhere in the post? Thank you anyway.Shame

© 2022 - 2024 — McMap. All rights reserved.