wowza and streaming. lesson 7

id.241224001.763829

Lesson 7: Transcoding with Wowza REST API

In this lesson, we’ll focus on configuring transcoding using the Wowza REST API, with code examples in Node.js and Python.


Step 1: Prerequisites

  1. Enable Transcoder:

    • In the Wowza Streaming Engine Manager, navigate to the application (e.g., LiveStreamApp).

    • Go to the Transcoder section and enable the Transcoder AddOn.

  2. API Authentication:

    • Ensure you have the REST API username and password.

Step 2: Configure a Transcoding Template

A transcoding template defines the output streams (e.g., different resolutions or bitrates). You can either create a custom template or modify an existing one.

Node.js Code: Create/Update Transcoder Template

  1. Install Dependencies:

     npm install axios
    
  2. Code:

     const axios = require('axios');
    
     const wowzaApiUrl = 'http://<server-ip>:8087/v2';
     const username = 'your-username';
     const password = 'your-password';
    
     async function updateTranscoderTemplate() {
         const templateConfig = {
             name: "custom-template",
             outputs: [
                 {
                     name: "720p",
                     videoBitrate: 2000000,
                     audioBitrate: 128000,
                     width: 1280,
                     height: 720,
                     codec: "H.264"
                 },
                 {
                     name: "480p",
                     videoBitrate: 1000000,
                     audioBitrate: 128000,
                     width: 854,
                     height: 480,
                     codec: "H.264"
                 }
             ]
         };
    
         try {
             const response = await axios.put(
                 `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/transcoder/templates/custom-template`,
                 templateConfig,
                 {
                     auth: { username, password },
                     headers: { 'Content-Type': 'application/json' }
                 }
             );
             console.log('Transcoder template updated:', response.data);
         } catch (error) {
             console.error('Error updating transcoder template:', error.response.data);
         }
     }
    
     updateTranscoderTemplate();
    

Python Code: Create/Update Transcoder Template

  1. Install Dependencies:

     pip install requests
    
  2. Code:

     import requests
     from requests.auth import HTTPBasicAuth
    
     wowza_api_url = 'http://<server-ip>:8087/v2'
     username = 'your-username'
     password = 'your-password'
    
     def update_transcoder_template():
         template_config = {
             "name": "custom-template",
             "outputs": [
                 {
                     "name": "720p",
                     "videoBitrate": 2000000,
                     "audioBitrate": 128000,
                     "width": 1280,
                     "height": 720,
                     "codec": "H.264"
                 },
                 {
                     "name": "480p",
                     "videoBitrate": 1000000,
                     "audioBitrate": 128000,
                     "width": 854,
                     "height": 480,
                     "codec": "H.264"
                 }
             ]
         }
    
         url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/transcoder/templates/custom-template"
         try:
             response = requests.put(
                 url,
                 json=template_config,
                 auth=HTTPBasicAuth(username, password)
             )
             if response.status_code == 200:
                 print("Transcoder template updated:", response.json())
             else:
                 print("Error:", response.status_code, response.json())
         except Exception as e:
             print("Error updating transcoder template:", e)
    
     update_transcoder_template()
    

Step 3: Start Transcoding

Node.js Code: Start Transcoding

async function startTranscoding() {
    const transcoderActionUrl = `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/instances/_definst_/transcoder/actions/start`;

    try {
        const response = await axios.put(
            transcoderActionUrl,
            {},
            {
                auth: { username, password },
                headers: { 'Content-Type': 'application/json' }
            }
        );
        console.log('Transcoding started:', response.data);
    } catch (error) {
        console.error('Error starting transcoder:', error.response.data);
    }
}

startTranscoding();

Python Code: Start Transcoding

def start_transcoding():
    transcoder_action_url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/instances/_definst_/transcoder/actions/start"

    try:
        response = requests.put(
            transcoder_action_url,
            auth=HTTPBasicAuth(username, password)
        )
        if response.status_code == 200:
            print("Transcoding started:", response.json())
        else:
            print("Error:", response.status_code, response.json())
    except Exception as e:
        print("Error starting transcoder:", e)

start_transcoding()

Step 4: Monitor Transcoding

Node.js Code: Monitor Transcoding

async function monitorTranscoding() {
    const transcoderStatusUrl = `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/instances/_definst_/transcoder`;

    try {
        const response = await axios.get(
            transcoderStatusUrl,
            {
                auth: { username, password },
                headers: { 'Content-Type': 'application/json' }
            }
        );
        console.log('Transcoder status:', response.data);
    } catch (error) {
        console.error('Error monitoring transcoder:', error.response.data);
    }
}

monitorTranscoding();

Python Code: Monitor Transcoding

def monitor_transcoding():
    transcoder_status_url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/instances/_definst_/transcoder"

    try:
        response = requests.get(
            transcoder_status_url,
            auth=HTTPBasicAuth(username, password)
        )
        if response.status_code == 200:
            print("Transcoder status:", response.json())
        else:
            print("Error:", response.status_code, response.json())
    except Exception as e:
        print("Error monitoring transcoder:", e)

monitor_transcoding()

Example Use Case

  1. Scenario:

    • You want to stream a live event and create two renditions: 720p and 480p.

    • Transcoding should be active and monitored programmatically.

  2. Steps:

    • Use the code to create/update the transcoding template.

    • Start the transcoder using Node.js or Python.

    • Monitor transcoder performance and outputs.


Next Steps:

  • Explore advanced transcoding features like audio-only streams or custom renditions.

  • Automate transcoder controls with event-driven logic.