-1
\$\begingroup\$

I'm currently trying to add text to a video. To do so I've been manipulating each frame by adding text to each bitmap and then rendering a new video.

using (var vFReader = new VideoFileReader())
{
    vFReader.Open(HostingEnvironment.MapPath("~/Content/images/video.mp4"));

    using (var vFWriter = new VideoFileWriter())
    {
        vFWriter.Open(HostingEnvironment.MapPath($"~/Content/videos/{id}.mp4"), vFReader.Width, vFReader.Height, vFReader.FrameRate, VideoCodec.H264, vFReader.BitRate);

        for (var i = 0; i < vFReader.FrameCount; i++)
        {
            var image = vFReader.ReadVideoFrame();

            var graphics = Graphics.FromImage(image);

            graphics.SmoothingMode = SmoothingMode.AntiAlias;

            graphics.DrawString(facebookUser?.FirstName, font, Brushes.White, new Point(vFReader.Width / 2, 75), format);
            //graphics.DrawString("PASSED YOU A MESSAGE", font, Brushes.White, new Point(vFReader.Width / 2, 170), format);

            graphics.Flush();

            vFWriter.WriteVideoFrame(image);
        }
        vFWriter.Flush();
    }
}

This is running rather slowly, so I was wondering if this is the wrong approach to take?

Or, is there a way to speed up the execution of my current code?

\$\endgroup\$
  • \$\begingroup\$ Though I can see the overlap, I'm not quite convinced CodeReview.SE is the correct place for this question (if I'm wrong, feel free to correct me). \$\endgroup\$ – Flater Apr 13 '18 at 11:15
  • \$\begingroup\$ Please post the complete method and some benchmarks. \$\endgroup\$ – t3chb0t Apr 13 '18 at 14:38
0
\$\begingroup\$

Reading a frame at a time from disk is going to be slow. Could you not read n frames, stash them in a buffer and then process them and write them back out.

The image that you are overlaying isn't changing, so could you not render the image onto a transparent background and then bitblt it to every frame.

Writing in bulk (like reading) might be quicker.

\$\endgroup\$
0
\$\begingroup\$

it will be very useful to know which library you are using, i found different results for VideoFileReader ( AForge maybe ? )

But in general it will be very useful to use async IO ( if they are available ) like FlushAsync(), WriteAsync and so on.

Another option could be splitting the reading - modifing - writing process into a flow of task https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-messages-to-and-read-messages-from-a-dataflow-block

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.