SoFunction
Updated on 2025-03-10

Code to implement flv video conversion in

In fact, it is implemented using Process objects in .Net.
   string str=@"d:\  d:\test_allen.flv";
   RunFFMpeg(str);



// Run FFMpeg's video decoding,
   public void RunFFMpeg(string strCmd)
   {
//Create and start a new process
       Process p = new Process();
//Set the process startup information attribute StartInfo, which is the ProcessStartInfo class, including some properties and methods:
= "";                                                             �
= " -i " + strCmd;    //Execution parameters
       ();
   }

//Run the Dos command and return the execution result
   public string RunCmd(string command)
   {
//Create and start a process
       Process p = new Process();

//The Process class has a StartInfo property, which is the ProcessStartInfo class, which includes some properties and methods, such as:
= "";                                                             �
= " /c " + command;    //Execution parameters
= false;         //Close the use of Shell
= true;   //Redirect standard input
= true;  //Redirecting standard output
= true;   //Redirect error output
= true;          //Set the window not to be displayed

       (); 
//(command);        //You can also enter the command to be executed in this way
//("exit");        //But remember to add Exit or the next program will be launched when the next program is executed

//The code that can end the process after automatic conversion is completed must be created.
return ();          //Get command execution results from the output stream
   }



------------------------------------
Another method: Use MEncoder to convert video files to Flv

Mencoder What to note when converting videos


1. Download and
2. Add the folder you are in to the environment variable Path, such as C:\mplayer
3. Microsoft key + R, enter cmd, and type in the following syntax on the command line
mencoder d:\ -o d:\ -of lavf  -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=100:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1 -vf scale=512:-3 -ofps 12 -srate 22050

Syntax description:
mencoder d:\ -o d:\  //Export D:\  to d:\, Parameter -o represents the output path
-of lavf    //Parameters -of : The format of the output file, the flv file belongs to the lavf format
-lavfopts i_certify_that_my_video_stream_does_not_use_b_frames     //Lavf format parameter settings. For converting flv format, it is best to add this long sentence. It means no bframes.

-oac mp3lame   //Output audio encoder, mp3lame used here
-lameopts abr:br=56   //It is a parameter setting specifically for mp3lame, abr:br=56, it is to set the audio bit rate to 56

-ovc lavc  //Output video encoder



//Video encoder settings:
-lavcopts 

vcodec=flv:     //Specify the video encoder as flv
vbitrate=500:  //The specified video bit rate is 500, the low quality is 150kb/s, the medium quality is 400kb/s, and the high quality is 700kb/s.
mbd=2:         //The macro module selection algorithm, the value is 0~2 and the default is 0. The larger the value, the slower the conversion, but it is beneficial in terms of quality and file size.
mv0:               //Compile each macro module and select the best one, which is invalid when mbd=0;
trell:           //It will slightly increase the quality, and the effect will be more obvious when mbd>0;
v4mv:               /Quantum grid search, find the optimal encoding for every 8×8block
cbp:                //It can only be used in the first phase with trell to evaluate the distorted image block encoding;
last_pred=3:   //The number of movements compared to the previous frame is predicted, with a value of 0~99, 1~3, and when it is greater than 3, it is no longer important to improve quality, but it will slow down the speed;
dia=4:           //Move search range, the value is -99~6. For fast conversion, -1 is a good value. If you don’t pay much attention to speed, you can consider 2~4;
cmp=6:           //The value is 1~2000, the default is 0, and the comparison function is set for the full pixel movement budget.
vb_strategy=1  //It will be helpful for scenes with large actions. For some videos, setting vmax_b_frames will cause loss of quality, and adding vb_strategy=1 will be better.

// Mencoder parameters
-vf scale=512:-3   //-vf means that the video format scale is scaled, 512:-3 means that the width is forced to be set to 512, and the height is written to -3 means that the aspect ratio can be maintained, or it can be set to -1 or -2.
-ofps 12              /*The frame rate of the output video. Generally, the frame rate of the video used for flash playback is meaningless when the frame rate is high, and it will increase the video file size, but if the ofps settings are not appropriate,
For example, the frame rate of the source video is not an integer multiple of ofps, which may cause the converted video and audio to be out of sync.
It seems that this parameter can be changed to -noskip to solve this problem; */

-srate 22050      //The audio sampling rate is generally 22050 or 44100.



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

Mencoder Chinese reference manual address

/DOCS/HTML/zh_CN/ 


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

Parameter description:

-srate 32000 : //Sound sampling rate, 22050hz
-af channels=2:2:1:0:1:1 //Right channel
-af channels=2:2:0:0:0:1 //Left channel
-stereo 0 //Stereo
-oac mp3lame ://The output audio is encoded using mp3
-lameopts mode=2:cbr:br=32://Bit rate br=32 kbit
-ovc xvid -xvidencopts bitrate=200 : //Encoding in xvid mode, with a code rate of 200kbps (After experiments, divx is currently faster than xvid, and it will slightly affect the speed when using the vhq function)

-vf scale=480:-3 -sws 9: //Convert to 480 pixels wide, and the height is scaled to scale.

"C:\" -o "C:\" //Source file address -o to the output file address

2> : //Encoding log record address




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

18fps: This is obtained through multiple tests before. 15fps for general TV series and cartoons is enough. If the scene change is not very large, 13fps can ensure smooth playback. Reducing the fps value can make the compression speed faster. For movies, especially action movies, to ensure smoothness and no mosaics, you must increase the fps, and a maximum of 24 is enough.

Choose 18fps to cope with most, with high cost performance

Audio sampling rate 32KHz: Inspiration from the recording device (the following quality description is a metaphor, the previous parameters are original)

32KHz - 32Kbps - Mono is better than phone
44KHz - 64Kbps - J-Stereo Radio Quality
44KHz -128Kbps - J-Stereo MP3 standard quality
44KHz -192Kbps - Stereo is close to CD quality

After many tests (the above is the interval range, you can choose according to your needs)
The most space-saving tablet pressing - Use 32KHz - 32Kbps - mono, which can ensure content;
The quality requirements are not high - 32KHz available - 128Kbps - mono high cost-effectiveness;
Large space and high quality - 44KHz - 192Kbps - Stereo for large SD card users.

Video sampling rate bitrate=150:

80 -  Minimum space occupancy, worst quality, slightly faster conversion speed (combined with the lowest audio, size can be 1min=1M)
122 - The small card used to configure the bottom machine is relatively cost-effective (mosaics will be added when changing faces)
150 - Meet the basic requirements for picture quality of the bottom configuration machine (perfect for non-action films)
248 - The economic choice of big card users (good quality)
350 - A machine with high configuration of large cards (economic reflects machine advantages)
Higher - Users can try it yourself

In general, the size of the output file is significantly affected by the video sampling rate and audio sampling rate, and the picture size and fps value also affect the size. How to set parameters depends on your personal needs and machine configuration.

In addition, there are many unknown parameters, please look for them.

For example: pess2, pess3, dynamic FPS, screen capture, time interval settings, etc.


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

About volume adjustment:

carlwang wrote: Personal habits do not use headphones, it is uncomfortable when the weather is hot, so I have always had a loud sound.
My experience: If the source of the film is DVDrip, it is generally above 5. If it is rmvb, it is usually 2. If it is rmvb, it will be popping.

-oac mp3lame -lameopts mode=2:cbr:br=64:vol=2 Let’s try it


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



The conversion will be displayed
Pos: 226.8s 297f (100%) 48fps Trem: 0min 5mb a-v:-0.009 [142:56] 
The content here should be the progress of the conversion in the brackets. The content in the brackets is very important. 142 represents the video bit rate, and 56 represents the audio bit rate (I don’t know if the word bit rate is used correctly...)

When the conversion is finished, 2 lines of text will be displayed:
Video stream: 142.564 kbit/s (17820 B/s) size: 4046677 bytes 227.080 secs 2700 frames 
Audio stream: 56.938 kbit/s (7117 B/s) size: 1616197 bytes 227.082 secs 
I think everyone can understand these 2 lines of information. What I have been paying attention to is the 142.564kbit/s.

If you have used flash8's own conversion tool, you should know that there is an option in the advanced settings that is the maximum data rate.
When the medium image quality is selected, it is 400kb/s, and the low quality is also 150kb/s. That is to say, the video converted with that command has the picture quality that is not as good as the quality. So, what is the quality?


Take a look at the command:
mencoder "D:\" -o "D:\" -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=150:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -srate 22050 

analyze:
-ovc > It is probably the abbreviation of output video codec. It formulates the output video encoding and chooses lavc. What does it mean? It is said that the libavcodec encoder was used;

-lavcopts > is the options option setting of lavc

You need to set the video encoding parameters!
codec=flv > It is about using the flv encoder, so let's ignore this;
vbitrate=150 > video bit rate! Video data rate! It's it! It turns out that I set 150!

Change it to vbitrate=1000, try again!
The result is exactly the same as the previous one...

Calm down and read the official document up. This parameter is the maximum data rate. It is the largest. How big is it? It seems that it is not up to it to control the quality.

Conclusion: There are two ways to set quality:
1. Set -lavcopts, see the command:
-lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1 

In -lavcopts, dia=4:cmp=6:vb_strategy=1, these 3 items
The larger the dia, the higher the quality. If you need to quickly encode it, set to -1 and set to 4 will be very beneficial to quality;
The larger the cmp, the higher the quality. The default value is 0, which is the fastest. Generally, setting it to 3 and setting it to 6 will only slightly improve the quality, but the speed will be slow.
vb_strategy, which is about 1 or 0, the default value may be 0. I set it to 1 here, which will be helpful for encoding. I haven't thought too much about this parameter myself, and it may not affect the quality...

2. Add the -sws option and see the following command:
-sws 3 
Just add a 3 to it, and the conversion effect is roughly the same as the previous method. The video data rate is about 422.5kb/s. Let's explain it
-sws is used to set quality, with the default value of 2, so why not use this simple method? I personally believe that using the previous method is conducive to finding a balance point between speed and quality, after all, the range of changes is relatively large.

Personal opinion:
In this way, it is best to use client conversion. After the conversion is completed, it is necessary to upload a small video upload software on the client.