VP8 Encoder (Run)¶
This article explains how you can use {transcoder-run-net} to encode a raw YUV video file to VP8 video in an IVF container.
The code snippets in this article are from the enc_vp8_file .NET sample.
Source Video¶
For source we use the foreman_qcif.yuv file from the AVBlocks Assets repository. After downloading and unzipping you will find foreman_qcif.yuv in the vid subdirectory.
Code¶
This code takes a raw YUV video file and encodes it to VP8 video in an IVF container.
Initialize AVBlocks¶
The first step in any AVBlocks application is to initialize the library. This must be done before using any other AVBlocks functionality. Always call Library.Shutdown() at the end of the program to clean up resources.
static int Main(string[] args)
{
var opt = new Options();
if (!opt.Prepare(args))
return opt.Error ? (int)ExitCodes.OptionsError : (int)ExitCodes.Success;
Library.Initialize();
bool encodeResult = Encode(opt);
Library.Shutdown();
return encodeResult ? (int)ExitCodes.Success : (int)ExitCodes.EncodingError;
}
Configure Input Socket¶
The input socket describes the raw YUV source file. The frame size, frame rate, and color format must match the source video.
static MediaSocket CreateInputSocket(Options opt)
{
MediaSocket socket = new MediaSocket();
socket.StreamType = StreamType.UncompressedVideo;
socket.File = opt.InputFile;
MediaPin pin = new MediaPin();
socket.Pins.Add(pin);
VideoStreamInfo vsi = new VideoStreamInfo();
pin.StreamInfo = vsi;
vsi.StreamType = StreamType.UncompressedVideo;
vsi.ScanType = ScanType.Progressive;
vsi.FrameWidth = opt.Width;
vsi.FrameHeight = opt.Height;
vsi.ColorFormat = opt.Color.Id;
vsi.FrameRate = opt.Fps;
return socket;
}
Configure Output Socket¶
The output socket writes VP8 video in an IVF container. The socket stream type is Ivf, and the output pin stream type is Vp8.
static MediaSocket CreateOutputSocket(Options opt)
{
MediaSocket socket = new MediaSocket();
socket.File = opt.OutputFile;
socket.StreamType = StreamType.Ivf;
MediaPin pin = new MediaPin();
socket.Pins.Add(pin);
VideoStreamInfo vsi = new VideoStreamInfo();
pin.StreamInfo = vsi;
vsi.StreamType = StreamType.Vp8;
return socket;
}
Configure and Run Transcoder¶
Create the sockets, enable demo mode, add the sockets to a transcoder, and run it. The sample removes an existing output because the transcoder intentionally fails when the output file already exists.
static bool Encode(Options opt)
{
DeleteFile(opt.OutputFile);
// Create output directory if needed
string outputDir = System.IO.Path.GetDirectoryName(opt.OutputFile);
if (!string.IsNullOrEmpty(outputDir))
System.IO.Directory.CreateDirectory(outputDir);
MediaSocket inSocket = CreateInputSocket(opt);
MediaSocket outSocket = CreateOutputSocket(opt);
// create Transcoder
using (Transcoder transcoder = new Transcoder())
{
transcoder.AllowDemoMode = true;
transcoder.Inputs.Add(inSocket);
transcoder.Outputs.Add(outSocket);
bool res = transcoder.Open();
PrintStatus("Transcoder open", transcoder.Error);
if (!res)
return false;
res = transcoder.Run();
PrintStatus("Transcoder run", transcoder.Error);
if (!res)
return false;
transcoder.Close();
PrintStatus("Transcoder close", transcoder.Error);
}
return true;
}
Complete Code¶
Here’s the complete working example that demonstrates VP8 encoding using AVBlocks for .NET.
using System;
using PrimoSoftware.AVBlocks;
namespace CliSample
{
class Program
{
static int Main(string[] args)
{
var opt = new Options();
if (!opt.Prepare(args))
return opt.Error ? (int)ExitCodes.OptionsError : (int)ExitCodes.Success;
Library.Initialize();
// Set license information. To run AVBlocks in demo mode, comment the next line out
// Library.SetLicense("<license-string>");
bool encodeResult = Encode(opt);
Library.Shutdown();
return encodeResult ? (int)ExitCodes.Success : (int)ExitCodes.EncodingError;
}
static bool Encode(Options opt)
{
DeleteFile(opt.OutputFile);
// Create output directory if needed
string outputDir = System.IO.Path.GetDirectoryName(opt.OutputFile);
if (!string.IsNullOrEmpty(outputDir))
System.IO.Directory.CreateDirectory(outputDir);
MediaSocket inSocket = CreateInputSocket(opt);
MediaSocket outSocket = CreateOutputSocket(opt);
// create Transcoder
using (Transcoder transcoder = new Transcoder())
{
transcoder.AllowDemoMode = true;
transcoder.Inputs.Add(inSocket);
transcoder.Outputs.Add(outSocket);
bool res = transcoder.Open();
PrintStatus("Transcoder open", transcoder.Error);
if (!res)
return false;
res = transcoder.Run();
PrintStatus("Transcoder run", transcoder.Error);
if (!res)
return false;
transcoder.Close();
PrintStatus("Transcoder close", transcoder.Error);
}
return true;
}
static void DeleteFile(string filename)
{
try
{
if (System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
}
catch { }
}
static MediaSocket CreateInputSocket(Options opt)
{
MediaSocket socket = new MediaSocket();
socket.StreamType = StreamType.UncompressedVideo;
socket.File = opt.InputFile;
MediaPin pin = new MediaPin();
socket.Pins.Add(pin);
VideoStreamInfo vsi = new VideoStreamInfo();
pin.StreamInfo = vsi;
vsi.StreamType = StreamType.UncompressedVideo;
vsi.ScanType = ScanType.Progressive;
vsi.FrameWidth = opt.Width;
vsi.FrameHeight = opt.Height;
vsi.ColorFormat = opt.Color.Id;
vsi.FrameRate = opt.Fps;
return socket;
}
static MediaSocket CreateOutputSocket(Options opt)
{
MediaSocket socket = new MediaSocket();
socket.File = opt.OutputFile;
socket.StreamType = StreamType.Ivf;
MediaPin pin = new MediaPin();
socket.Pins.Add(pin);
VideoStreamInfo vsi = new VideoStreamInfo();
pin.StreamInfo = vsi;
vsi.StreamType = StreamType.Vp8;
return socket;
}
static void PrintStatus(string action, ErrorInfo e)
{
if (action != null)
{
Console.Write("{0}: ", action);
}
if (ErrorFacility.Success == e.Facility)
{
Console.WriteLine("Success");
return;
}
else
{
Console.WriteLine("{0}, facility:{1} code:{2} hint:{3}", e.Message ?? "", e.Facility, e.Code, e.Hint ?? "");
}
}
enum ExitCodes : int
{
Success = 0,
OptionsError = 1,
EncodingError = 2,
}
}
}
How to Run¶
See the enc_vp8_file .NET sample for details.
Command Line¶
enc_vp8_file --frame <width>x<height> --rate <fps> --color <color> --input <file.yuv> --output <file.ivf> [--colors]
Examples¶
List options:
./bin/net10.0/enc_vp8_file --help
Encode a raw YUV video from ./assets/vid/foreman_qcif.yuv to a VP8 video in ./output/enc_vp8_file/foreman_qcif.ivf:
# Linux and macOS
mkdir -p ./output/enc_vp8_file
./bin/net10.0/enc_vp8_file \
--input ./assets/vid/foreman_qcif.yuv \
--output ./output/enc_vp8_file/foreman_qcif.ivf \
--frame 176x144 \
--rate 30 \
--color yuv420
# Windows
mkdir -Force -Path ./output/enc_vp8_file
./bin/net10.0/enc_vp8_file `
--input ./assets/vid/foreman_qcif.yuv `
--output ./output/enc_vp8_file/foreman_qcif.ivf `
--frame 176x144 `
--rate 30 `
--color yuv420