Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Was some good practise for me here the code(tested locally with java and open cv 3.4.1)

    String videoInputFile = "in.mp4";
    VideoCapture reader = new VideoCapture(videoInputFile);
    double fps = reader.get(Videoio.CAP_PROP_FPS);
    double width = reader.get(Videoio.CAP_PROP_FRAME_WIDTH);
    double height = reader.get(Videoio.CAP_PROP_FRAME_HEIGHT);
    //load image and resize for video
    Mat img = imread("img.jpg");
    resize(img, img, new Size(width, height));

    //carfully with choosing codec and output extension - could be that you need to install codecs
    String outputFileName = "out.avi";
    VideoWriter writer = new VideoWriter();
    int fourcc = VideoWriter.fourcc('M','J','P','G');
    writer.open(outputFileName, fourcc, fps, new Size(width, height));

    //write img on first position
    writer.write(img);
    Mat frame = new Mat();
    //just copy the video
    while(reader.read(frame)) {
        writer.write(frame);
    }
    writer.release();
    reader.release();

Was some good practise for me me(i am currently i that area) here the code(tested locally with java and open cv 3.4.1)3.4.1) - translate to c++ or python if u like.

    String videoInputFile = "in.mp4";
    VideoCapture reader = new VideoCapture(videoInputFile);
    double fps = reader.get(Videoio.CAP_PROP_FPS);
    double width = reader.get(Videoio.CAP_PROP_FRAME_WIDTH);
    double height = reader.get(Videoio.CAP_PROP_FRAME_HEIGHT);
    //load image and resize for video
    Mat img = imread("img.jpg");
    resize(img, img, new Size(width, height));

    //carfully with choosing codec and output extension - could be that you need to install codecs
    String outputFileName = "out.avi";
    VideoWriter writer = new VideoWriter();
    int fourcc = VideoWriter.fourcc('M','J','P','G');
    writer.open(outputFileName, fourcc, fps, new Size(width, height));

    //write img on first position
    writer.write(img);
    Mat frame = new Mat();
    //just copy the video
    while(reader.read(frame)) {
        writer.write(frame);
    }
    writer.release();
    reader.release();