FFmpeg video conversion and screenshot

Source: Internet
Author: User
Tags flv file

FFmpeg is a tool for video conversion. A few days ago, I wanted to make a video conversion and function to understand this stuff. Visit http://www.ffmpeg.com.cn/index.php/?e9=a6%96%e9%a1%b5.

I don't know much about it, but I have implemented the two features I want. I would like to share them with my friends who want this feature.

1. First download the FFMPEG file.

2. Video conversion function. You can convert multiple video formats to FLV formats. I have tested WMA, RM, Avi, MOD and so on.

 

Code

///   <Summary>
/// Convert video (AVI, mov, etc.) to FLV format
///   </Summary>
///   <Param name = "fromname"> Converted video file </Param>
///   <Returns> </returns>
Public   String Videoconvertflv ( String Fromname)
{
String FFmpeg = Server. mappath ( " . " ) +   " \ FFMPEG \ ffmpeg.exe " ;
String Exportname =   " FLV "   +   " \\ "   + Datetime. Now. tostring ( " Yyyymmddhhmmss " ) +   " . FLV " ;
String Widthandheight =   " 320x240 " ;
System. Diagnostics. processstartinfo startinfo =   New System. Diagnostics. processstartinfo (FFMPEG );
Startinfo. windowstyle = System. Diagnostics. processwindowstyle. hidden;
Startinfo. Arguments =   " -I "   + Server. mappath (fromname) +   " -AB 56-ar 22050-B 500-R 15-S "   + Widthandheight +   "   "   + Server. mappath (exportname );
Try
{
System. Diagnostics. process. Start (startinfo );
Return Exportname;
}
Catch (Exception ERR)
{
Return Err. message;
}
}

 

When calling the API, enter the path of the original file. The relative path is used in the above method.

 

Protected   Void Btnconvert_click ( Object Sender, eventargs E)
{
String Result = Videoconvertflv ( " Upload \ training1.avi " );
Response. Write (result );
}

 

 

 

2. Video: captures thumbnails of a selected video at a specified time.

 

Code

///   <Summary>
/// Capture a frame from the video screen as an image
///   </Summary>
///   <Param name = "videoname"> Video file PIC/Guiyu. mov </Param>
///   <Param name = "widthandheight"> The image size is 240*180. </Param>
///   <Param name = "cuttimeframe"> The start time of the screenshot, for example, "1" </Param>
///   <Returns> </returns>
Public   String Getpicfromvideo ( String Videoname, String Widthandheight, String Cuttimeframe)
{

StringFFmpeg=Server. mappath (".")+ "\ FFMPEG \ ffmpeg.exe";

String Picname = Server. mappath ( " IMG \\ "   + Datetime. Now. tostring ( " Yyyymmddhhmmss " ) +   " . Jpg " );
System. Diagnostics. processstartinfo startinfo =   New System. Diagnostics. processstartinfo (FFMPEG );
Startinfo. windowstyle = System. Diagnostics. processwindowstyle. hidden;
Startinfo. Arguments =   " -I "   + Server. mappath (videoname) +   " -Y-F image2-SS "   + Cuttimeframe +   " -T 0.001-S "   + Widthandheight +   "   "   + Picname;
Try
{
System. Diagnostics. process. Start (startinfo );
Return Picname;
}
Catch (Exception ERR)
{
Return Err. message;
}
}

 

Method parameters can be adjusted according to the actual situation. The call method is as follows:

 

String URL = Getpicfromvideo ( " Upload \ training1.avi " , " 240*180 " , " 1 " );

 

 

Now, the function of converting a video to FLV and capturing a video thumbnail is realized.

-------------------------------------------------------

Continued:

Because the video upload and conversion process takes some time, the method called during this time will fail. Because the FLV file is not yet generated. This situation generally seems to be easy to encounter when processing multiple threads. In this case, experience is still insufficient, that is, it feels that it is a sequential call, and multithreading is not used .. In fact, it is still not completely thorough. If you have a clear expert, give me some advice.

Later, I checked some information and finally found thatCodeYou can:

Before modification:

 

Try
{
System. Diagnostics. process. Start (startinfo );
Return Picname;
}

 

 

After modification:

 

 

Try
{
Process proc = Process. Start (startinfo );
Proc. waitforexit ();
Return Exportname;
}

 

In this way, you can controlProgramStep by step.

 

At the requirement of yuanyou, I made a compressed package for the code to learn together./Files/Janes/video.7z
Note: because the size of the ffmpeg.exe file exceeds the upload size limit, please download the file and put it under the FFMPEG directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.