Skip to main content

YouTube streaming

The YouTube RTMP output functionality in VisioForge SDKs allows you to stream video content directly to YouTube using various video and audio encoders. This guide will walk you through setting up and configuring YouTube streaming in your application.

Cross-platform YouTube streaming (X-engines: VideoCaptureCoreX, VideoEditCoreX, Media Blocks)

The YouTubeOutput class provides a flexible interface for configuring YouTube streaming settings, including:

  • Video encoder selection and configuration
  • Audio encoder selection and configuration
  • Custom video and audio processing
  • YouTube-specific sink settings

Basic Setup

To begin streaming to YouTube, you'll need your YouTube stream key. Here's how to create a basic YouTube output configuration:

// Initialize YouTube output with your stream key
var youtubeOutput = new YouTubeOutput("your-youtube-stream-key");

Video Encoder Configuration

The YouTube output supports multiple video encoders, optimized for different hardware configurations:

Available Video Encoders

  • OpenH264 (Software-based encoding)
  • NVENC H264 (NVIDIA GPU acceleration)
  • QSV H264 (Intel Quick Sync)
  • AMF H264 (AMD GPU acceleration)
  • HEVC/H265 variants (where supported)

By default, the system will use OpenH264 on most platforms, with Apple Media H264 being used on macOS. You can manually configure a different encoder based on your requirements:

// Example: Using NVIDIA NVENC encoder if available
if (NVENCH264EncoderSettings.IsAvailable())
{
youtubeOutput.Video = new NVENCH264EncoderSettings();
}

Video Encoder Settings

Each encoder allows you to configure various parameters. Here's an example of configuring common encoding settings:

var videoSettings = new OpenH264EncoderSettings
{
Bitrate = 4500000, // 4.5 Mbps
KeyframeInterval = 60, // Keyframe every 2 seconds at 30fps
// Add other encoder-specific settings as needed
};
youtubeOutput.Video = videoSettings;

Audio Encoder Configuration

The system supports multiple AAC audio encoders:

Available Audio Encoders

  • VO-AAC (Default for non-Windows platforms)
  • AVENC AAC
  • MF AAC (Windows-only)
// Example: Configure audio encoder settings
var audioSettings = new VOAACEncoderSettings
{
Bitrate = 128000, // 128 kbps
SampleRate = 48000 // 48 kHz
};
youtubeOutput.Audio = audioSettings;

Platform-Specific Considerations

Windows

  • Has access to Media Foundation (MF) encoders
  • Supports additional HEVC/H265 encoding options
  • Default audio encoder is MF AAC

macOS

  • Uses Apple Media H264 encoder by default
  • Uses VO-AAC for audio encoding

Other Platforms

  • Defaults to OpenH264 for video
  • Uses VO-AAC for audio encoding

Best Practices

  1. Encoder Selection

    • Always check encoder availability before using hardware accelerated options
    • Fall back to OpenH264 if hardware acceleration is unavailable
  2. Stream Settings

    • Follow YouTube's recommended bitrates for your resolution
    • Use appropriate keyframe intervals (2 seconds is common)
    • Match audio sample rate to YouTube's requirements (48 kHz recommended)
  3. Error Handling

    • Implement proper error handling for stream connection issues
    • Monitor encoder performance and stream health

Example Implementation

Here's a complete example showing how to set up YouTube streaming with error handling:

VideoCaptureCoreX/VideoEditCoreX sample

try
{
var youtubeOutput = new YouTubeOutput("your-stream-key");

// Configure video encoding
if (NVENCH264EncoderSettings.IsAvailable())
{
youtubeOutput.Video = new NVENCH264EncoderSettings
{
Bitrate = 4500000,
KeyframeInterval = 60
};
}

// Configure audio encoding
youtubeOutput.Audio = new MFAACEncoderSettings
{
Bitrate = 128000,
SampleRate = 48000
};

// Additional sink settings if needed
youtubeOutput.Sink.CustomProperty = "value";

// Add the output to the video capture instance
core.Outputs_Add(youtubeOutput, true); // core is an instance of VideoCaptureCoreX

// Or set the output for the video edit instance
videoEdit.Output_Format = youtubeOutput; // videoEdit is an instance of VideoEditCoreX
}
catch (Exception ex)
{
// Handle initialization errors
Console.WriteLine($"Failed to initialize YouTube output: {ex.Message}");
}

Media Blocks SDK sample

// Create the YouTube sink block (using RTMP)
var youtubeSinkBlock = YouTubeSinkBlock(new YouTubeSinkSettings("streaming key"));

// Connect the video encoder to the sink block
pipeline.Connect(h264Encoder.Output, youtubeSinkBlock.CreateNewInput(MediaBlockPadMediaType.Video));

// Connect the audio encoder to the sink block
pipeline.Connect(aacEncoder.Output, youtubeSinkBlock.CreateNewInput(MediaBlockPadMediaType.Audio));

Troubleshooting

Common issues and their solutions:

  1. Encoder Initialization Failures

    • Verify hardware encoder availability
    • Check system requirements for chosen encoder
    • Confirm proper driver installation for hardware encoders
  2. Stream Connection Issues

    • Verify stream key validity
    • Check network connectivity
    • Confirm YouTube server status
  3. Performance Problems

    • Monitor CPU/GPU usage
    • Adjust bitrate and encoding settings
    • Consider switching to hardware acceleration if available

Additional Resources