سؤال

I have some mp4 video file and i want to set in this video file some small icon(can be simple bitmap).

I want to write some application that the input is mp4/mpeg file and some icon or bitmap and the output is the same mp4 with this icon embed in the left corner.

How to do it ? Is there some code example for this?

هل كانت مفيدة؟

المحلول

You can use a lot of libraries to accomplish such a thing, I will show you a way to do it using C++ and OpenCV.

You basically need to do three things:

  1. You need to decode the input video file
  2. You need to process each frame in order to add your icon/image in its corner
  3. You need to re-encode the modified frame, and save it into a new video file

OpenCV is a library that specializes in image processing. It can help you accomplish task #2. It also has some functionality that allows you to read and write video files, but it's not as advanced as other libraries that specialize in encoding/decoding video files (like ffmpeg).

I have created a sample that uses OpenCV to read a video file (wmv), and add to each frame an image file (jpg). The result will be another video file (avi). OpenCV can also read other types of video files (mp4), but I couldn't get it to save an .mp4 file, I could only make it save an .avi file.

In order to use my sample you need to setup OpenCV in your platform and IDE of choice. You can find information about how to do this on the OpenCV website.

In my sample I use the MJPEG encoder to save the video file. If you replace CV_FOURCC('M','J','P','G') with -1 a window will pop up that will ask you what encoder you want to use. If you want to use another encoder, just put -1 in there.

Here is the code that I used:

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void addImageInCorner(Mat videoFrame, Mat resizedImage)
{
    // The destination location of the image in the frame (upper-left corner)
    Rect destinationRect(0, 0, resizedImage.cols, resizedImage.rows);
    // Copy the image to the specified location in the frame
    resizedImage.copyTo(videoFrame(destinationRect));
}

void main()
{
    string inputVideoFile = "C:\\Users\\Public\\Videos\\Sample Videos\\Wildlife.wmv";
    string inputImageFile = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg";
    string outputVideoFile = "C:\\Users\\Public\\Videos\\Sample Videos\\output.avi";

    // Read image file
    Mat image = imread(inputImageFile);
    Mat resizedImage;
    resize(image, resizedImage, Size(400, 300));

    // Open video capture
    VideoCapture capture;
    capture.open(inputVideoFile);

    if (!capture.isOpened())
    {
        return;
    }

    // Get the capture properties
    double fps = capture.get(CV_CAP_PROP_FPS);
    int videoWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH);
    int videoHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT);

    // Initialize the video writer
    VideoWriter writer;
    writer.open(outputVideoFile, CV_FOURCC('M','J','P','G'), fps, Size(videoWidth, videoHeight));

    Mat currentFrame;
    namedWindow("video");

    // While the window is still open
    while (cvGetWindowHandle("video") != NULL)
    {
        // Read the next frame
        capture >> currentFrame;

        if (currentFrame.empty())
        {
            // If the frame could not be read, exit the loop
            break;
        }

        // Add the image to the corner of the video frame
        addImageInCorner(currentFrame, resizedImage);

        writer << currentFrame;

        // Show the image in the video window
        imshow("video", currentFrame);
        waitKey(10);
    }

    writer.release();
}

Here is the resulting image: Result image frame

I hope this helps you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top