Adding images to the end of the video

Kaushik DB

common_video.avi image1.jpg image2.jpg

i want to insert these two images to the end of the video name common_video.avi programatically in c# so that the image shows for like 5 seconds after the video ends, what's the best way to achieve it? I have looked in to ffmpeg, with and without c# wrappers, but still nothing works. I keep getting errors and exceptions.

here's a piece of code i have tried

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
//using AForge;
//using AForge.Video.VFW;
using AForge.Video.FFMPEG;
using AviFile;
using System.Drawing;

namespace Ffmpeg
{
    class Program
    {
        static void Main(string[] args)
        {






            var Strings = new string[] { "1.jpg", "2.jpg", "3.jpg" };

            //VideoFileWriter W = new VideoFileWriter();
            //W.Open("../../Out/new.avi", 1920, 1200, 1, VideoCodec.Raw, 4400);
            //foreach (var S in Strings)
            //{
            //    for (int I = 2 * 25; I > 0; I--)
            //        W.WriteVideoFrame((Bitmap)Image.FromFile(S));
            //}
            //W.Close();



            //load the first image
            Bitmap bitmap = (Bitmap)Image.FromFile(Strings[0]);
            //create a new AVI file
            AviManager aviManager =
                new AviManager(@"..\..\out\new.avi", false);
            //add a new video stream and one frame to the new file
            VideoStream aviStream =
                aviManager.AddVideoStream(false, 1, bitmap);

            //Bitmap bitmap;
            for (int n = 1; n < Strings.Length; n++)
            {
                if (Strings[n].Trim().Length > 0)
                {
                    bitmap =
                       (Bitmap)Bitmap.FromFile(Strings[n]);
                    //for (int I = 2 * 25; I > 0; I--)
                        aviStream.AddFrame(bitmap);
                    bitmap.Dispose();
                }
            }
            aviManager.Close();
        }
    }
}

Ffmpeg throws: "error configuring filters";

George Y.

Not sure how the FFmpeg C API translates to C#, but adding a video stream in FmMpeg does not let you add more frames at the end of existing video stream. It creates a new video stream, therefore generating a video file with several video streams. Not only this is not what you want, but also not all muxers support muxing two video streams which may be the reason you're getting an error.

What you need could be achieved one of the following way:

  1. Decode the original video, and reencode it. During reencoding you can easily modify the video in any way, and add images anywhere you want. This is the easiest to implement, but you'll lose the video quality due to dual reencoding if you encode into a lossy format.

  2. Find out the exact parameters which were used to encode the original video (resolution, aspect rate, colorspace, frame rate, codec, codec parameters etc), then encode your images using exactly the same codec with the same options. Then you can copy the video stream by reading/writing frames (no reencoding), and append your newly encoded frames to this video. This will avoid reencoding of the original video, but is much trickier to implement and prone to errors, especially if the original video was encoded not by FFmpeg but a different encoder.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related