Slinfen: C #. NET using FFmpeg operation video Combat (format conversion, add watermark one step)

Source: Internet
Author: User

Ffmpeg.exe is a famous video processing software that runs as a command line parameter. There is also a lot of information about ffmpeg on the Internet. But in the practical development of C #, but encountered a few problems and considerations, such as how to deal with the non-destructive video? How can I add a watermark while converting the format to improve processing efficiency? , what version should I choose for the ffmpeg version? Today Slin will explore the mysteries of C # operation FFmpeg in a practical way.

About the use of ffmpeg and its parameter commands, here is not too much to introduce. Mainly in the project to combat the main.

Due to work needs, the author has nearly 300 short videos to deal with, in the Internet to find a lot of tools, although can use, but there is a kind of hold the feeling. It is either a software watermark or a piece of flower after processing, or it can not be processed directly in batches, the video should be set one by one.

The main requirement here is to give the existing video format conversion, if the video format has met the requirements, directly in the specified location watermark (PNG image), after processing, in order to resolve disk space, after the video processing is completed to delete the original video. The author of C # language is the most well-known, so choose C # WinForm to do a simple video batch processing software.

Go ahead with a completed project:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/871423/201601/871423-20160102120409323-1354510578. JPG "style=" border:0px; "/>

Read the video in the specified directory, and then a processing can be (in the middle of the number of seconds to intercept the parameters, belong to the video cut, temporarily do not have this function)

Existing videos are in FLV format, call ffmpeg in C #, convert to MP4 format, and add watermarks

c# calls FFmpeg method encapsulation as follows:

 <summary>///  Video Processor Ffmpeg.exe location/// </summary>public string ffmpegpath  { get; set; } /// <summary>///  call ffmpeg.exe  Execute command/// </ Summary>/// <param name= "Parameters" > Command parameters </param>/// <returns> return execution Results </ Returns>private string runprocess (string parameters) {//Create a ProcessStartInfo object   and set related properties Var oinfo = new processstartinfo (ffmpegpath, parameters); OInfo.UseShellExecute  = false;oinfo.createnowindow = true;oinfo.redirectstandardoutput = true;o info.redirectstandarderror = true;oinfo.redirectstandardinput = true; // Creates a string and streamreader  used to get the processing result string output = null; streamreader sroutput = null; try{    //call FFmpeg to start processing commands      var proc = process.start (oinfo);     &Nbsp;proc. WaitForExit ();       //gets the output stream     sroutput = proc. standarderror;     //conversion into string    output =  Sroutput.readtoend ();      //Close Handler     proc. Close ();} catch  (Exception) {    output = string. Empty;} finally{    //Release Resources     if  (Sroutput != null)      {        sroutput.close ();         sroutput.dispose ();     }}return output;}

Conversion Format command parameters:-I orignal.flv-y-b 1024k-acodec copy-f mp4 Newfile.mp4

Command parameters for adding watermarks:-I orignal.flv-i water.png-filter_complex \ "Overlay=10:10\" newfile.flv

Parameter brief description and detail hints:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none RGB ( 221,221,221); Background-color:rgb (255,255,255); "/>

ORIGNAL.FLV: The original video file to be processed (preferably absolute path)-y: Overwrite the existing file (note that the watermark does not overwrite the original file, otherwise it can only generate 1 seconds of video)-B: The bitrate of the video is set here 1024k Basic can meet the non-destructive processing if the-b parameter is not set by default to 200k The video will be very blurry-acodec copy: Keep audio quality constant-F mp4: Represents the video format of the conversion-I water.png: Watermark picture path overlay=10:10: Watermark distance video upper left corner coordinates other position parameters: upper right corner: Main_w-overla Y_w-10:10 lower left corner: 10:main_h-overlay_h-10 lower right corner: main_w-overlay_w-10:main_h-overlay_h-10 newfile.mp4 file path to save

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none RGB ( 221,221,221); Background-color:rgb (255,255,255); "/>

The above approach is core processing. In the process of actual execution, the author discovers the following problems:

When using the CMD window to execute the above command (CMD in front of the parameter to add ffmpeg attention to file location), can be successfully processed, but when running WinForm test, found that only a new size of 0kb files generated, but delay processing. Give people a phenomenon of suspended animation. And when the author off debugging WinForm program, after a few seconds, seemingly ffmpeg.exe and function, file processing success. This is not the solution. (When the handler is called, a new thread is executed)

Troubleshoot the situation:

May be a ffmpeg version issue, so downloaded 2.8.2 version (should be up-to-date), test, no changes

Check the program's call flow to display the call procedure cmd window. The result is blank, nothing, still no effect.

Finally after a variety of information to find, inadvertently saw someone said Proc. WaitForExit (); The execution of this sentence causes the program to remain in a waiting state. Yes, that's true, it was done with similar program calls before, and there's no such thing. So, with a try attitude, commented on this sentence. Of course, the program no longer waits for execution to complete, Proc. Close (); This sentence should also be commented. Test Results Success!! (Daniel, who understands the underlying principles, tells one or two)

The problem is solved, but there is a problem with the efficiency: how to deal with it faster?

I tried a combination of various commands, found that for different versions of FFmpeg, some parameters are not used, the author used 2.8. Version 2 finally found a better solution:

You can choose to use the following command parameters:

-I orignal.flv-i water.png-filter_complex \ "overlay=10:10\"-y-b 1024k-acodec copy-f mp4

-I orignal.flv-i water.png-filter_complex \ "Overlay=10:10\"-B 1024k-acodec Copy

The above one is suitable for simultaneous conversion of format and watermark

The following one is suitable for watermark only, do not format conversion

These core problems are solved, and the rest is the file read, save, judge and so on details.

summary:                                  ,         &NB Sp              
    1. C # do not use proc when calling FFmpeg. WaitForExit (); method, otherwise it will feign death

    2. ffmpeg version is best to use the latest version, and refer to the command parameter description

    3. lossless conversion, lossless and watermark to pay attention to ensure the video bitrate and audio parameters (direct copy, video can not write-avcodec copy   will error, can only set video bitrate with-B)  

    4. one step processing method (simultaneous watermark of conversion, refer to above command parameter)

After the development of the program, the author does not have to be forced to a one to set up, processing, computer open, monitor off, only to hear the host screaming babies processing, and so finish the meal, all things have been done ...


This article is from "Slin's personal blog" blog, make sure to keep this source http://jayshsoft.blog.51cto.com/11066123/1730854

Slinfen: C #. NET using FFmpeg operation video Combat (format conversion, add watermark one step)

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.