4

I'm trying to add text to a video. I've been using the Accord framework so far, and this is what I currently have:

using (var vFReader = new VideoFileReader())
{
    vFReader.Open(@"\video.mp4");

    using (var vFWriter = new VideoFileWriter())
    {
        vFWriter.Open(@"\video2.mp4", vFReader.Width, vFReader.Height, vFReader.FrameRate, VideoCodec.MPEG4, vFReader.BitRate);

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

            var graphics = Graphics.FromImage(image);

            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.DrawString("Custom text", font, Brushes.White, new Point(vFReader.Width / 2, 25), format);

            graphics.Flush();

            vFWriter.WriteVideoFrame(image, (uint)i);
        }
        vFWriter.Flush();
    }
}

This is technically working, but the resulting video is in very poor quality compared to the original. What am I doing wrong?

Here is the difference in quality.

Original video:

Original video

Edited video:

enter image description here

Frederik Hansen
  • 506
  • 4
  • 21
  • Can you add a side-by-side comparison of the input and output videos? Is the quality just as bad if you just save the original image without adding text? – Manfred Radlwimmer Apr 11 '18 at 07:43
  • @ManfredRadlwimmer I have added screenshots of the quality difference. Also, the quality is just as bad if I don't edit the bitmap in any way. – Frederik Hansen Apr 11 '18 at 07:55
  • 1
    Either the codec that's being used for encoding isn't very good or this is simply a side-effect of re-encoding the video. You might need to bump up the bitrate or find a better codec. – Manfred Radlwimmer Apr 11 '18 at 08:01
  • The original video is probably not uncompressed so each iteration will lose a bit of quality. Here is an extreme example of the same thing happening with Jpegs that's basically the same effect but more noticable: https://youtu.be/NzsbjwuWYYI – Manfred Radlwimmer Apr 11 '18 at 08:05
  • Thank you @ManfredRadlwimmer. I tried switching to H264-codec and doubling the bit-rate and the quality is much better now! – Frederik Hansen Apr 11 '18 at 08:06
  • 1
    Glad I could help :) – Manfred Radlwimmer Apr 11 '18 at 08:07

0 Answers0