Upscale Video

How to scale up a 480p (854x480) video to Full HD 1080p (1920x1080) video.

The code snippets in this article are from the simple_video_upscale .NET sample.

Source Video

For a source video we use the MP4 file from the TED talk video What’s the next window into our universe? by Andrew Connolly. The original video format is Wide 480p or 16:9, 854 x 480.

Code

This code takes an MP4 file with H.264 video and AAC audio, and scales the video stream up to Full HD 1920x1080 (1080p) using cubic interpolation for best upscaling quality. The audio stream is copied from the source as is.

Initialize AVBlocks and Configure Transcoder

The sample uses hardcoded input and output file paths. It creates a MediaInfo object to probe the input file, then creates input and output sockets. The output socket is cloned from the input socket, then the frame size is set to 1920x1080 and the interpolation method is set to Cubic.

static void Main(string[] args)
{
    Library.Initialize();

    string inputPath = "AndrewConnolly_2014.mp4";
    string outputPath = "AndrewConnolly_2014_1080p.mp4";

    try
    {
        using (var mediaInfo = new MediaInfo())
        {
            mediaInfo.Inputs[0].File = inputPath;
            if (!mediaInfo.Open())
            {
                PrintError("Open MediaInfo", mediaInfo.Error);
                return;
            }

            var inputSocket = MediaSocket.FromMediaInfo(mediaInfo);

            var outputSocket = (MediaSocket)inputSocket.Clone();
            outputSocket.File = outputPath;

            var outVideoPin = outputSocket.Pins[0];

            var outVideoStreamInfo = outVideoPin.StreamInfo as VideoStreamInfo;
            if (outVideoStreamInfo != null)
            {
                outVideoStreamInfo.FrameWidth = 1920;
                outVideoStreamInfo.FrameHeight = 1080;
            }

            outVideoPin.Params.Add(Param.Video.Resize.InterpolationMethod, (int)InterpolationMethod.Cubic);

            using (var transcoder = new Transcoder())
            {
                transcoder.AllowDemoMode = true;

                transcoder.Inputs.Add(inputSocket);
                transcoder.Outputs.Add(outputSocket);

                if (transcoder.Open())
                {
                    transcoder.Run();
                    transcoder.Close();
                }
                else
                {
                    PrintError("Transcoder Open", transcoder.Error);
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception: {ex.Message}");
    }

    Library.Shutdown();
}

PrintError Helper

static void PrintError(string action, ErrorInfo error)
{
    if (action != null)
        Console.Write("{0}: ", action);

    if (ErrorFacility.Success == error.Facility)
    {
        Console.WriteLine("Success");
        return;
    }
    else
    {
        Console.WriteLine("{0}, facility:{1} code:{2} hint:{3}",
            error.Message ?? "", error.Facility, error.Code, error.Hint ?? "");
    }
}

Complete Code

Here’s the complete working example that demonstrates video upscaling using AVBlocks for .NET.

using System;
using PrimoSoftware.AVBlocks;

class Program
{
    static void Main(string[] args)
    {
        Library.Initialize();

        string inputPath = "AndrewConnolly_2014.mp4";
        string outputPath = "AndrewConnolly_2014_1080p.mp4";

        try
        {
            using (var mediaInfo = new MediaInfo())
            {
                mediaInfo.Inputs[0].File = inputPath;
                if (!mediaInfo.Open())
                {
                    PrintError("Open MediaInfo", mediaInfo.Error);
                    return;
                }

                var inputSocket = MediaSocket.FromMediaInfo(mediaInfo);

                var outputSocket = (MediaSocket)inputSocket.Clone();
                outputSocket.File = outputPath;

                var outVideoPin = outputSocket.Pins[0];

                var outVideoStreamInfo = outVideoPin.StreamInfo as VideoStreamInfo;
                if (outVideoStreamInfo != null)
                {
                    outVideoStreamInfo.FrameWidth = 1920;
                    outVideoStreamInfo.FrameHeight = 1080;
                }

                outVideoPin.Params.Add(Param.Video.Resize.InterpolationMethod, (int)InterpolationMethod.Cubic);

                using (var transcoder = new Transcoder())
                {
                    transcoder.AllowDemoMode = true;

                    transcoder.Inputs.Add(inputSocket);
                    transcoder.Outputs.Add(outputSocket);

                    if (transcoder.Open())
                    {
                        transcoder.Run();
                        transcoder.Close();
                    }
                    else
                    {
                        PrintError("Transcoder Open", transcoder.Error);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Exception: {ex.Message}");
        }

        Library.Shutdown();
    }

    static void PrintError(string action, ErrorInfo error)
    {
        if (action != null)
            Console.Write("{0}: ", action);

        if (ErrorFacility.Success == error.Facility)
        {
            Console.WriteLine("Success");
            return;
        }
        else
        {
            Console.WriteLine("{0}, facility:{1} code:{2} hint:{3}",
                error.Message ?? "", error.Facility, error.Code, error.Hint ?? "");
        }
    }
}

How to Run

See the simple_video_upscale .NET sample for details.

Command Line

This sample uses hardcoded input and output file paths — no command line parsing.

bin/net10.0/simple_video_upscale

Examples

Build the sample from the repository root:

dotnet build samples.sln

Download the sample video:

cd samples/simple_video_upscale
curl -L -o AndrewConnolly_2014.mp4 \
    https://archive.org/download/AndrewConnolly_2014/AndrewConnolly_2014.mp4

Run the sample (the input/output file paths are relative to the working directory):

# Linux and macOS
../../bin/net10.0/simple_video_upscale
# Windows
..\..\bin\net10.0\simple_video_upscale.exe

The upscaled output file AndrewConnolly_2014_1080p.mp4 will be created in the samples/simple_video_upscale directory.