Home > Article > Backend Development > golang remove video watermark
With the development of the Internet, video sharing and dissemination have become an important part of people's daily lives. However, in many cases, we often encounter watermark problems when watching videos. In order to improve our viewing experience, this article will introduce how to use Golang to remove watermarks and watch videos without watermarks.
1. The concept of watermark
Watermark refers to a kind of image, text and other information embedded in digital media (such as videos, pictures, documents, etc.) to protect its copyright and Prevent piracy. Common watermarks include transparent watermarks, text watermarks, picture watermarks, etc. When watching videos, watermarks will affect the viewing effect and even the look and feel of the video content.
2. What is Golang?
Golang is a popular programming language that was developed by Google in 2009 and officially released in 2012. Golang is characterized by concise syntax, strong concurrency, high efficiency, and good security. Therefore, it has been widely used in back-end development, distributed systems, network programming and other fields.
3. The principle of removing video watermark
Now that we know what watermark is, let’s take a look at the principle of removing watermark. Generally, the position and content of the watermark are fixed, so we can delete or change the watermark pixels through video editing software or image processing algorithms. Although this method is more effective, it also has some shortcomings, such as the need for manual processing or low operating efficiency.
In this article, we will introduce a way to use Golang to write a program to remove video watermarks. The specific implementation principle is to process video frames, use image processing algorithms to find the location and size of the watermark, and perform operations such as pixel replacement, to ultimately achieve the purpose of removing the watermark.
4. Implementation steps
Next, we will introduce the implementation steps of how to use Golang to remove video watermarks.
Before starting to write the program, we need to install Golang's image processing library and video processing library, which includes video decoding and frame processing. support. The installation command is as follows:
go get -u github.com/disintegration/gift
go get -u github.com/3d0c/gmf
We need to read the video file through the GMF library and decode each video frame into an image for further processing. The sample code is as follows:
inputContext, _ := gmf.NewInputCtx(inputFile)
defer inputContext.Free()
videoStream, _ := inputContext.GetBestStream(gmf. AVMEDIA_TYPE_VIDEO)
videoCodec, _ := gmf.FindEncoder(gmf.AV_CODEC_ID_RAWVIDEO)
videoFrame := gmf.NewFrame()
videoSwCtx := gmf.NewSwCtx()
videoSwCtx.SetSize(videoStream.CodecCtx().Width(), videoStream.CodecCtx().Height())
videoSwCtx.SetPixelFormat(gmf.AV_PIX_FMT_RGB24)
videoSwCtx .SetSrcDims(videoStream.CodecCtx().Width(), videoStream.CodecCtx().Height())
videoSwCtx.SetDstDims(videoStream.CodecCtx().Width(), videoStream.CodecCtx().Height ())
videoSwCtx.Init()
if err := videoCodec.Open(nil); err != nil {
log.Panicln("Cannot open video codec:", err)
}
for packet := range inputContext.GetNewPackets() {
if packet.StreamIndex() == videoStream.Index() {
frame, err := packet.Decode(videoStream.CodecCtx())
if err != nil {
log.Panicln("Cannot decode packet:", err)
}
if frame != nil {
videoSwCtx.Scale(frame, videoFrame)
//Write the processed frame
}
}
}
Next, we need to process each Process video frames and use the gift library to modify and enhance the images. In it, we need to find the location and size of the watermark in order to do things like pixel replacement. The sample code is as follows:
filter := gift.New(
gift.Resize(videoStream.CodecCtx().Width(), videoStream.CodecCtx().Height(), gift.LanczosResampling ),
gift.Convolution(
[]float32{
-1, -1, -1,
-1, 9, -1 ,
-1, -1, -1,
},
false, false, false, 0,
),
)
watermark, _ := gift.Open("watermark.png")
for {
frame, err := videoStream.ReadPacket()
if err != nil {
if err == io.EOF {
break
}
log.Panicln("Cannot read packet:", err)
}
videoFrame.SetPts(frame.Pts())
videoSwCtx.Scale(frame, videoFrame)
watermarkMask := gift.NewPolarMask(watermark.Bounds(), watermark.Bounds().Center(), 0.5)
maskSize := gift.Resize(watermark.Bounds().Dx()/2, watermark.Bounds().Dy()/2, gift.LanczosResampling)
watermark = maskSize.Resample(watermark, nil)
watermark = gift.Mask(watermark, watermarkMask)
filter.DrawAt(videoFrame, watermarkedFrame, image.Point{0, 0}, gift.OverOperator)
}
After processing, we need to use the GMF library to re-encode the modified video frames and write them to a new file. The sample code is as follows:
outputContext, _ := gmf.NewOutputCtx(outputFile)
defer outputContext.Free()
outputStream := outputContext.NewStream(codec)
if err := outputStream.CodecCtx().SetPixFmt(gmf.AV_PIX_FMT_YUV420P); err != nil {
log.Panicln("Cannot set codec pixel format")
}
if err := outputStream.CodecCtx().SetWidth(videoStream.CodecCtx().Width()); err != nil {
log.Panicln("Cannot set codec width")
}
if err := outputStream.CodecCtx().SetHeight(videoStream.CodecCtx().Height()); err != nil {
log.Panicln("Cannot set codec height")
}
if err := outputStream.CodecCtx().SetTimeBase(avrational.AVR{Num: 1, Den: 25}); err != nil {
log.Panicln("Cannot set codec time base")
}
if err := outputStream.CodecCtx().SetProfile(gmf.FF_PROFILE_H264_HIGH_444_PREDICTIVE); err != nil {
log.Panicln("Cannot set codec profile:", err)
}
if err := outputStream.CodecCtx().Open(nil); err != nil {
log.Panicln("Cannot open codec:", err)
}
swFrameCtx := gmf.NewSwCtx()
swFrameCtx.SetSize(videoStream.CodecCtx().Width(), videoStream.CodecCtx().Height())
swFrameCtx.SetPixelFormat(gmf.AV_PIX_FMT_YUV420P)
swFrameCtx.SetSrcDims(videoStream.CodecCtx().Width(), videoStream.CodecCtx().Height())
swFrameCtx.SetDstDims(videoStream.CodecCtx().Width(), videoStream.CodecCtx().Height())
swFrameCtx.Init()
for i := 0; i < len(watermarkedFrames); i {
swFrameCtx.Scale(watermarkedFrames[i], outputStream.CodecCtx(), gmf.SWS_FAST_BILINEAR)
pkt, err := outputStream.CodecCtx().Encode(watermarkedFrames[i])
if err != nil {
log.Panicln("Cannot encode frame:", err)
}
pkt.SetPts(int64(i))
if err := outputStream.WritePacket(pkt); err != nil {
log.Panicln("Cannot write packet:", err)
}
pkt.Free()
}
outputContext.CloseOutputAndNullify()
五、总结
本文介绍了如何用Golang编写程序去除视频水印,并提供了实现步骤和示例代码。这种方式相对于传统的手工处理和基于图像处理算法的方式更加高效和准确,并且代码实现相对简单。
但是,在实际使用中,我们也需要注意保护被去除水印的视频版权和知识产权,避免侵犯他人权益。
The above is the detailed content of golang remove video watermark. For more information, please follow other related articles on the PHP Chinese website!