Upscale Video

This article explains how to scale a 480p (854x480) video up to Full HD 1080p (1920x1080) video.

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 bicubic method for interpolation. The audio stream is copied from the source as is.

.NET

using PrimoSoftware.AVBlocks;
using System.Collections.Generic;

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

            // start with two identical input and output configuration
            var inputInfo = new MediaInfo() {
                InputFile = "AndrewConnolly_2014.mp4"
            };

            var outputInfo = new MediaInfo() {
                InputFile = "AndrewConnolly_2014.mp4"
            };

            if (inputInfo.Load() && outputInfo.Load()) {

                // create input socket
                var inputSocket = MediaSocket.FromMediaInfo(inputInfo);

                // create output socket
                var outputSocket = MediaSocket.FromMediaInfo(outputInfo);
                outputSocket.File = "AndrewConnolly_2014_1080p.mp4";

                // set the new frame width and height to 1920 x 1080
                var outVideoStream = outputSocket.Pins[0].StreamInfo as VideoStreamInfo;
                outVideoStream.FrameWidth = 1920;
                outVideoStream.FrameHeight = 1080;

                // set the resize interpolation method: 
                var outVideoPin = outputSocket.Pins[0];
                outVideoPin.Params = new Dictionary<string, object>() {
                    { Param.Video.Resize.InterpolationMethod, InterpolationMethod.Cubic } 
                };

                // create and run a transcoder
                using (var transcoder = new Transcoder()) {
                    transcoder.Inputs.Add(inputSocket);
                    transcoder.Outputs.Add(outputSocket);

                    // process
                    if (transcoder.Open())
                    {
                        transcoder.Run();
                        transcoder.Close();
                    }
                }
            }

            Library.Shutdown();
        }
    }
}

How to run

Follow the steps to create a C# console application in Visual Studio, but in Program.cs use the code from this article.

Download the AndrewConnolly_2014.mp4 MPEG4 file from the Internet Archive and save it in bin\x64\Debug under the project's directory.

Run the application in Visual Studio. Wait a few seconds for the Transcoder to finish. The converted file AndrewConnolly_2014_1080p.mp4 will be in the bin\x64\Debug directory.

C++

Windows

// UpscaleVideo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace primo;
using namespace primo::codecs;
using namespace primo::avblocks;

int _tmain(int argc, _TCHAR* argv[])
{
    Library::initialize();

    // start with two identical input and output configurations
    ref<MediaInfo> inputInfo (Library::createMediaInfo());
    inputInfo->setInputFile(L"AndrewConnolly_2014.mp4");

    ref<MediaInfo> outputInfo (Library::createMediaInfo());
    outputInfo->setInputFile(L"AndrewConnolly_2014.mp4");

    if (inputInfo->load() && outputInfo->load()) {

        // create input socket
        ref<MediaSocket> inputSocket (Library::createMediaSocket(inputInfo.get()));

        // create output socket
        ref<MediaSocket> outputSocket (Library::createMediaSocket(outputInfo.get())); {
            outputSocket->setFile(L"AndrewConnolly_2014_1080p.mp4");

            // get output video pin
            auto outVideoPin = outputSocket->pins()->at(0); {
                // set the new frame width and height to 1920 x 1080
                auto outVideoStream = static_cast<VideoStreamInfo*>(outVideoPin->streamInfo()); {
                    outVideoStream->setFrameWidth(1920);
                    outVideoStream->setFrameHeight(1080);
                }

                // set the resize interpolation method:
                // InterpolationMethod::Cubic is best for upscaling (however, it is slow)
                ref<ParameterList> outVidePinsParams (Library::createParameterList()); {
                    ref<IntParameter> interpolationMethod (Library::createIntParameter()); {
                        using namespace Param::Video;
                        interpolationMethod->setName(Resize::InterpolationMethod);
                        interpolationMethod->setValue(InterpolationMethod::Cubic);
                    }

                    outVidePinsParams->add(interpolationMethod.get());
                }

                outVideoPin->setParams(outVidePinsParams.get());
            }
        }

        // create a Transcoder
        ref<Transcoder> transcoder (Library::createTranscoder()); {
            // add input and output sockets
            transcoder->inputs()->add(inputSocket.get());
            transcoder->outputs()->add(outputSocket.get());

            // process
            if (transcoder->open()) {
                transcoder->run();
                transcoder->close();
            }
        }
    }

    Library::shutdown();

    return 0;
}
How to run

Follow the steps to create a C++ console application in Visual Studio, but use the code from this article.

Download the AndrewConnolly_2014.mp4 MPEG4 file from the Internet Archive and save it in the project directory.

Run the application in Visual Studio. Wait a few seconds for the Transcoder to finish. The converted file AndrewConnolly_2014_1080p.mp4 will be in the project directory.


Last updated on April 8th, 2017 02:55:54 PM