美文网首页
利用opencv为视频添加动态字幕

利用opencv为视频添加动态字幕

作者: 此间不留白 | 来源:发表于2020-12-23 21:44 被阅读0次

在之前的文章中,介绍了如何利用OpenCV在图片中插入图形和文字的方法。本篇文章主要介绍了如何opencv为视频添加动态字幕。

利用opencv编辑视频的主要原理是通过将视频帧解析为opencv 矩阵的格式实现的,通过计算帧频率和帧数量,可以很容易实现为视频添加动态字幕的功能,其实现代码如下:

#include<iostream>
#include<opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp> 
#include"putText.h"
#include<vector>
#include<string>
using namespace cv;

/*
* 
* 每隔30帧写入一个汉字,“青山依旧在,几度夕阳红”
*
*/
void processFrame(Mat m,const char* text,Point pp)
{
    if (!m.empty())
    {
        putTextZH(m, text, pp, Scalar(130, 220, 238), 100, "迷你简启体");
    }
    
}
int main()
{
    //加载视频
    VideoCapture capture;
    Mat frame;
    int count = 0;
    frame = capture.open("test.mp4");
    
    const char *text1[]= {"青","青\n山","青\n山\n依","青\n山\n依\n旧","青\n山\n依\n旧\n在" };
    const char *text2[] = {"几","几\n度","几\n度\n夕","几\n度\n夕\n阳","几\n度\n夕\n阳\n红" };
    //获取帧率
    std::cout << "帧率" << capture.get(CAP_PROP_FPS)<<std::endl;
    int fnum = capture.get(CAP_PROP_FRAME_COUNT);
    int currentFrame = capture.get(CAP_PROP_POS_FRAMES);
    std::cout << "当前帧:" << currentFrame << std::endl;
    VideoWriter writer;
    writer.open("end2.mp4", writer.fourcc('M', 'P', '4', 'V'), 30.0, Size(1150,750), true);
    
    while (currentFrame<fnum)
    {
        
        Mat m;
        Mat m1, m2;
        capture >> frame;
        if (currentFrame >= 0 && currentFrame <= 30)
        {
            processFrame(frame, text1[0], Point(200, 100));
            frame.copyTo(frame);
            //continue;
            std::cout << "当前帧" << currentFrame << std::endl;
        }
            
        else if (currentFrame > 30 && currentFrame <= 60)
        {
            processFrame(frame, text1[1], Point(200, 100));
            //frame.copyTo(frame);
            //continue;
        }
        else if (currentFrame > 60 && currentFrame <= 90)
        {
            processFrame(frame, text1[2], Point(200, 100));
            //frame.copyTo(frame);
            //continue;
        }
        else if (currentFrame > 90 && currentFrame <= 120)
        {
            processFrame(frame, text1[3], Point(200, 100));
            //frame.copyTo(frame);
            //continue;
        }
        else if (currentFrame > 120 && currentFrame <= 150)
        {
            processFrame(frame, text1[4], Point(200, 100));
            //frame.copyTo(frame);
            //continue;
            writer.write(frame);
            waitKey(10);
        }
        else if (currentFrame > 150 && currentFrame <= 180)
        {
            processFrame(frame, text1[4], Point(200, 100));
            processFrame(frame, text2[0], Point(350, 100));
            //frame.copyTo(frame);
            //continue;
        }
        else if (currentFrame > 180 && currentFrame <= 210) 
        {
            processFrame(frame, text1[4], Point(200, 100));
            processFrame(frame, text2[1], Point(350, 100));
            //frame.copyTo(frame);
            //continue;
        }
        else if (currentFrame > 210 && currentFrame <= 240)
        {
            processFrame(frame, text1[4], Point(200, 100));
            processFrame(frame, text2[2], Point(350, 100));
            //frame.copyTo(frame);
            //continue;
        }
        else if (currentFrame > 240 && currentFrame <= 270)
        {

            processFrame(frame, text1[4], Point(200, 100));
            processFrame(frame, text2[3], Point(350, 100));
            //frame.copyTo(frame);
            //continue;
        }
        else if (currentFrame > 270 && currentFrame <= 300)
        {
            processFrame(frame, text1[4], Point(200, 100));
            processFrame(frame, text2[4], Point(350, 100));
            //frame.copyTo(frame);
            //imshow("tex",frame);
            //waitKey(0);
            //continue;
        }
        else if(currentFrame>=300 && currentFrame< fnum)
        {
             processFrame(frame, text1[4], Point(200, 100));
             //frame.copyTo(frame);
             processFrame(frame, text2[4], Point(350, 100));
            
            //frame.copyTo(frame);
            
        }
        else
        {
            break;
            
        }

        currentFrame += 1;
        //imwrite( std::to_string(currentFrame) + ".PNG", frame);
        //frame.resize(800, 600);
        Mat frameSize;
        resize(frame, frameSize, Size(int(1150), int(750)));
        writer.write(frameSize);
        
        //imshow("frame", frame);
        waitKey(3);
        
        
        
    }
    

    
    waitKey(0);
    writer.release();
    capture.release();
    return 0;
}

其实现效果如下:

受到平台限制,对opencv生成的视频做了GIF处理和压缩,画质有所下降。

相关文章

网友评论

      本文标题:利用opencv为视频添加动态字幕

      本文链接:https://www.haomeiwen.com/subject/edwunktx.html