Convert video to FLV format

Source: Internet
Author: User

I have completed this demo recently. Let's share it with you!
The above gave me two days to complete this small function
At that time, I spent half a day rolling out documents from the network and debugging the materials.CodeAfter the success, I finally had the following experiences:

Important:
1. Two tools are used. One is ffmpeg.exe, and the other is mencoder.exe.
FFmpeg Latest Version: http://ffdshow.faireal.net/mirror/ffmpeg/
Mencoder New: http://www5.mplayerhq.hu/MPlayer/releases/win32/

Here is a key point:ArticleI didn't mention it, so some people didn't respond after running, because the download path above has many versions. Different versions may have different parameters, the parameters used in online articles are written in earlier versions, so they will be ineffective due to parameter errors after running.
The simple solution is to run the online parameter on the CMD command line. In this case, the command line will report a parameter error and delete it!

2. Determine whether the processing is successful or failed, or whether the progress is complete. Determine the output information obtained asynchronously [including obtaining the width and height of the original video]
The focus here is on two delegate events. For details, see the following code lines:

Code
1
2   Private Void Startprocess ( String Tool)
3 {
4 Startprocess (tool, False );
5 }
6 Private Void Startprocess ( String Tool, Bool Onlycheckinfo)
7 {
8 System. Diagnostics. PROCESS p =   New System. Diagnostics. Process ();
9 P. startinfo. filename = Tool;
10 P. startinfo. Arguments = Commandpara;
11 P. startinfo. useshellexecute =   False ;
12 P. startinfo. redirectstandardinput =   True ;
13 P. startinfo. redirectstandardoutput =   True ;
14 P. startinfo. redirectstandarderror =   True ;
15 P. startinfo. createnowindow =   False ;
16 P. outputdatareceived + =   New System. Diagnostics. datareceivedeventhandler (p_outputdatareceived );
17 If (Onlycheckinfo) // Only checks whether the file can be converted and obtained to the internal width and height.
18 {
19 P. errordatareceived + =   New System. Diagnostics. datareceivedeventhandler (p_checkinfodatareceived );
20 }
21 Else
22 {
23 P. errordatareceived + =   New System. Diagnostics. datareceivedeventhandler (p_errordatareceived );
24 }
25 // Start execution
26 Try
27 {
28 P. Start ();
29 P. beginoutputreadline ();
30 P. beginerrorreadline ();
31 P. waitforexit ();
32 }
33 Catch (Exception ERR)
34 {
35 Console. writeline (ERR. Message );
36 }
37 Finally
38 {
39 P. Close ();
40 }
41 }
42 Void P_checkinfodatareceived ( Object Sender, system. Diagnostics. datareceivedeventargs E)
43 {
44 If ( ! String . Isnullorempty (E. Data ))
45 {
46 If (E. Data. Contains ( " Stream " ) && E. Data. Contains ( " Video: " )) // Set the size of the original video window as the width and height of the FLV video.
47 {
48 Match match = RegEx. Match (E. Data, @" , (\ D +) x (\ D +) " );
49 If (Match ! =   Null )
50 {
51 Videowidth = Match. Groups [ 1 ]. Value;
52 Videoheight = Match. Groups [ 2 ]. Value;
53 }
54 }
55 Else   If (E. Data. Contains ( " Cocould not find codec Parameters " )) // FFmpeg Conversion failed
56 {
57 Iscanchangetoflv =   False ;
58 Program. setdatabase (ID, 1 , Count +   1 );
59 }
60 }
61
62 }
63
64 Void P_errordatareceived ( Object Sender, system. Diagnostics. datareceivedeventargs E)
65 {
66 If ( ! String . Isnullorempty (E. Data ))
67 {
68 If (E. Data. Contains ( " Video: " ) && E. Data. Contains ( " Muxing overhead " )) // FFmpeg conversion completed
69 {
70 Program. setdatabase (ID, 2 , Count +   1 );
71 Console. writeline ( " Conversion completed " );
72 }
73 Console. writeline (E. data );
74 }
75
76 }
77
78 Void P_outputdatareceived ( Object Sender, system. Diagnostics. datareceivedeventargs E)
79 {
80 If ( ! String . Isnullorempty (E. Data ))
81 {
82 If (E. Data. Contains ( " Writing Index " )) // Mencoder conversion completed
83 {
84 Program. setdatabase (ID, 2 , Count +   1 );
85 Console. writeline ( " Conversion completed " );
86 }
87 // Else if (E. Data. Contains ("exiting ")) // Mencoder Conversion failed
88 // {
89 // Console. writeline ("Conversion failed ");
90 // }
91 Console. writeline (E. data );
92 }
93 }
94
95

This article only focuses on this. Please refer to other articles on the Internet and this article!

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.