Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VideoWriter to write videos in C++

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

C++ how to use VideoWriter to write video, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this to learn, I hope you can gain something.

VideoWriter

Creation of classes

VideoWriter objects are created in two ways:

The first is to use the constructor form:

cv::VideoWriter out(

const string& filename, //Enter filename

int fourcc, //encoding form, using CV_FOURCC() macro

double fps, //output video frame rate

cv::Size frame_size, //Size of single frame picture

bool is_color = true //If false, gray image can be passed

);

The second way to use open():

cv::VideoWriter out;

out.open(

"my_video.mpg", //output file name

CV_FOURCC('D ',' I','V',' X'), // MPEG-4 encoding

30.0, //Frame Rate (FPS)

cv::Size( 640, 480 ), //Single frame image resolution of 640 x 480

true //Enter color map only

);

CV_FOURCC can get encoding formats

CV_FOURCC('P', 'I', 'M', '1') = MPEG-1 code

CV_FOURCC('M', 'J', 'P', 'G') = motion-jpeg codec

CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec

CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec

CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec

CV_FOURCC('U', '2', '6', '3') = H263 codec

CV_FOURCC('I', '2', '6', '3') = H263I codec

CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec

MPEG-1 is a video and audio compression format customized for CD media;

Motion JPEG is a video compression format in which each frame of an image is encoded separately using JPEG;

MPEG-4 uses very narrow bandwidth to compress and transmit data through frame reconstruction technology in order to obtain the best image quality with the least data.

video writing

VideoWriter object writes images

cv::VideoWriter::write(

const Mat& image //Write image as next frame

);

We have another way, which is ">" operation

my_video_writer >> my_frame;

The material we use below, the video material we started with last time, is "Dumping the World," and the image material we used to use often.

Target

We write the above image into the video we play, for example, in the 15th frame, the 30th frame, the 50th frame, and the 65th frame of the video, write our image, and then play it out to see.

video effects

realization idea

Load our images as Mat images.

Load video via VideoCapture Get FPS and width and height to video

Zoom our image to the width and height obtained in the previous step

Creating VideoWriter Objects

Play the video loaded by VideoCapture, write each frame obtained into the object of VideoWriter, and write the picture loaded by our Mat when judging the 15th frame, the 30th frame, the 50th frame and the 65th frame;

Release resources.

code writing

Create a new project opencv-1003, configure properties (VS2017 configure OpenCV common properties), and then write #include and main methods in the source file

1. Load our images as Mat images

2. Load the video through VideoCapture to obtain the FPS, width and height of the video, and calculate the time between two frames

3. Zoom our image to the width and height obtained in the previous step

4. Creating VideoWriter Objects

When we created the above, we directly defined it as a test.avi file under the E disk Family folder, and the video format was MJPG.

5. Play the video loaded by VideoCapture, write each frame obtained into the object of VideoWriter, and write the picture loaded by our Mat when judging the 15th frame, the 30th frame, the 50th frame and the 65th frame;

6. release resources

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report