Use ffmpeg in ASP. NET to implement video watermarks

Source: Internet
Author: User

I 've made a lot of detours in video watermarking, mainly because the related information is too messy. Some people say ffmpeg + avisynth is acceptable, but I don't want to install avisynth, finally, I read an article by a foreigner and finally got it okay. I am reminding you that the vhook parameter has long been used. The new version is not-vfilters, but-vf, the ffmpeg version I used here is the FFmpeg version: 2013-03-22 git-e0e8c20, and its watermark parameters are:

Top left corner
Ffmpeg-I inputvideo. avi-vf "movie?watermarklogo.png [watermark]; [in] [watermark] overlay = [out]" outputvideo. flv

Top right corner
Ffmpeg-I inputvideo. avi-vf "movie?watermarklogo.png [watermark]; [in] [watermark] overlay = main_w-overlay_w-10: 10 [out]" outputvideo. flv

Bottom left corner
Ffmpeg-I inputvideo. avi-vf "movie?watermarklogo.png [watermark]; [in] [watermark] overlay = 10: main_h-overlay_h-10 [out]" outputvideo. flv

Bottom right corner
Ffmpeg-I inputvideo. avi-vf "movie?watermarklogo.png [watermark]; [in] [watermark] overlay = main_w-overlay_w-10: main_h-overlay_h-10 [out]" outputvideo. flv

Use in ASP. NET:


[Csharp]
// Add watermark, fileName video address, imgFile watermark image address, outputFile output address
Public int WaterMark (string fileName, string imgFile, string outputFile)
{
// Obtain the path of ffmpeg.exe. The path is configured in Web. Config, for example, <add key = "ffmpeg" value = "E: \ aspx1 \ ffmpeg.exe"/>
String ffmpeg = Server. MapPath (PublicMethod. ffmpegbin );
If ((! System. IO. File. Exists (ffmpeg) | (! System. IO. File. Exists (fileName )))
{
Return 0;
}
// Create the ffmpeg Process
System. Diagnostics. ProcessStartInfo WaterMarkstartInfo = new System. Diagnostics. ProcessStartInfo (ffmpeg );
// Run in the background
WaterMarkstartInfo. WindowStyle = System. Diagnostics. ProcessWindowStyle. Hidden;
// Running parameters
String config = "-I" + fileName + "-vf \" movie = "+ imgFile +" [watermark]; [in] [watermark] overlay = 0: 0 [out] \ "" + outputFile;
WaterMarkstartInfo. Arguments = config;
Try
{
// Start Watermarking
System. Diagnostics. Process. Start (WaterMarkstartInfo );
}
Catch
{
Return 0;
}

Return 1;
}

// Add watermark, fileName video address, imgFile watermark image address, outputFile output address
Public int WaterMark (string fileName, string imgFile, string outputFile)
{
// Obtain the path of ffmpeg.exe. The path is configured in Web. Config, for example, <add key = "ffmpeg" value = "E: \ aspx1 \ ffmpeg.exe"/>
String ffmpeg = Server. MapPath (PublicMethod. ffmpegbin );
If ((! System. IO. File. Exists (ffmpeg) | (! System. IO. File. Exists (fileName )))
{
Return 0;
}
// Create the ffmpeg Process
System. Diagnostics. ProcessStartInfo WaterMarkstartInfo = new System. Diagnostics. ProcessStartInfo (ffmpeg );
// Run in the background
WaterMarkstartInfo. WindowStyle = System. Diagnostics. ProcessWindowStyle. Hidden;
// Running parameters
String config = "-I" + fileName + "-vf \" movie = "+ imgFile +" [watermark]; [in] [watermark] overlay = 0: 0 [out] \ "" + outputFile;
WaterMarkstartInfo. Arguments = config;
Try
{
// Start Watermarking
System. Diagnostics. Process. Start (WaterMarkstartInfo );
}
Catch
{
Return 0;
}

Return 1;
}
Isn't it easy? But when the program is debugged and running, you will find that ffmpeg cannot find the image of your watermark at all:

 

After my individual debugging, the problem is locked in:-vf "movie?watermarklogo.png [watermark]; [in] [watermark] overlay = [out]" where movie = is followed by only the relative path, for example, for/data/watermarklogo.png, once C:/data/watermarklogo.png is used, it cannot be found. If it is a window program, it is okay to put watermarklogo.png in the directory where the program is located and use the relative path. What if it is Web? I believe you may have thought of setting WorkingDirectory. That's right. The fake image is in C:/data/watermarklogo.png. Let's write it like this:


[Csharp]
WaterMarkstartInfo. WorkingDirectory = "C: \ data \\";

WaterMarkstartInfo. WorkingDirectory = "C: \ data \\";
Try it, right? After several days, the problem was finally solved.

However, after adding a watermark, we will find that the video quality is seriously degraded. In fact, the above command is just a simple watermark command. To ensure the video quality, the command should be as follows:

Ffmpeg.exe-y-I input. avi-acodec copy-B 300 k-vf "movie?watermarklogo.png [watermark]; [in] [watermark] overlay = 0: 0 [out]" output. avi

-Y indicates that output. flv with the same name exists and is overwritten directly.
-I input. avi indicates the video to be added to the watermark.
-Acodec copy indicates that the audio remains unchanged.
-B 300 k indicates the bit rate of the video to be processed. An error is reported when-vcodec copy is used. You can use other tools to obtain the original video bit rate and add it here to keep the bit rate unchanged, otherwise, the default value is 200 kb, and the video is lossy.
-Vf "..." is the watermark processing parameter in the middle, mainly overlay. The first parameter indicates the distance from the watermark to the left of the video, and the second parameter indicates the distance from the watermark to the top of the video.
Output. avi processed video

Add the watermark here! If you have any questions, please contact us!


 

Related 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.