Loading...
Searching...
No Matches
Video editing

Prev Tutorial: Font rendering
Next Tutorial: Custom Source and Sink

Original author Amir Hassan (kallaballa) amir@.nosp@m.viel.nosp@m.-zu.o.nosp@m.rg
Compatibility OpenCV >= 4.7

Render text on top of a video

Through adding a source and a sink v4d becomes capable of video editing. Reads a video, renders text on top and writes the result. Note: Reading and writing of video-data is multi-threaded in the background for performance reasons.

Video Editing
Downloading...
using namespace cv;
using namespace cv::v4d;
class VideoEditingPlan : public Plan {
cv::UMat frame_;
const string hv_ = "Hello Video!";
cv::Size fbSz_;
public:
void setup(Ptr<V4D> win) override {
fbSz_ = win->fbSize();
}
void infer(Ptr<V4D> win) override {
//Capture video from the source
win->capture();
//Render on top of the video
win->nvg([](const Size& sz, const string& str) {
using namespace cv::v4d::nvg;
fontSize(40.0f);
fontFace("sans-bold");
fillColor(Scalar(255, 0, 0, 255));
textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
text(sz.width / 2.0, sz.height / 2.0, str.c_str(), str.c_str() + str.size());
}, fbSz_, hv_);
//Write video to the sink (do nothing in case of WebAssembly)
win->write();
}
};
int main(int argc, char** argv) {
//In case of WebAssembly
CV_UNUSED(argc);
CV_UNUSED(argv);
Ptr<V4D> window = V4D::make(960, 960, "Video Editing");
#ifndef __EMSCRIPTEN__
//Make the video source
auto src = makeCaptureSource(window, argv[1]);
//Make the video sink
auto sink = makeWriterSink(window, argv[2], src->fps(), cv::Size(960, 960));
//Attach source and sink
window->setSource(src);
window->setSink(sink);
#else
//Make a webcam Source
auto src = makeCaptureSource(window);
//Attach webcam source
window->setSource(src);
#endif
window->run<VideoEditingPlan>(0);
}
Template class for specifying the size of an image or rectangle.
Definition: types.hpp:335
_Tp height
the height
Definition: types.hpp:363
_Tp width
the width
Definition: types.hpp:362
Definition: mat.hpp:2432
Definition: v4d.hpp:68
std::shared_ptr< _Tp > Ptr
Definition: cvstd_wrapper.hpp:23
Definition: nvg.hpp:20
Definition: backend.hpp:15
cv::Ptr< Sink > makeWriterSink(cv::Ptr< V4D > window, const string &outputFilename, const float fps, const cv::Size &frameSize)
cv::Ptr< Source > makeCaptureSource(cv::Ptr< V4D > window, const string &inputFilename)
"black box" representation of the file storage associated with a file on disk.
Definition: core.hpp:106