在之前的文章中,介紹了如何利用OpenCV
在圖片中插入圖形和文字的方法。本篇文章主要介紹了如何opencv
為視頻添加動(dòng)態(tài)字幕。
利用opencv
編輯視頻的主要原理是通過(guò)將視頻幀解析為opencv 矩陣的格式實(shí)現(xiàn)的激率,通過(guò)計(jì)算幀頻率和幀數(shù)量铁追,可以很容易實(shí)現(xiàn)為視頻添加動(dòng)態(tài)字幕的功能季蚂,其實(shí)現(xiàn)代碼如下:
#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幀寫(xiě)入一個(gè)漢字,“青山依舊在,幾度夕陽(yáng)紅”
*
*/
void processFrame(Mat m,const char* text,Point pp)
{
if (!m.empty())
{
putTextZH(m, text, pp, Scalar(130, 220, 238), 100, "迷你簡(jiǎn)啟體");
}
}
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陽(yáng)","幾\n度\n夕\n陽(yáng)\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 << "當(dāng)前幀:" << 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 << "當(dāng)前幀" << 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;
}
其實(shí)現(xiàn)效果如下:
受到平臺(tái)限制扭屁,對(duì)
opencv
生成的視頻做了GIF處理和壓縮算谈,畫(huà)質(zhì)有所下降。