wowza and streaming . lesson 8
id.241224001.589723
Lesson 8: Managing Stream Files with Wowza REST API
This lesson focuses on using the Wowza REST API to manage stream files. Stream files allow you to store and manage pre-configured connection settings for live streams. We will write code examples in Node.js and Python to create, update, start, and delete stream files.
Step 1: Prerequisites
Enable REST API in Wowza:
- Make sure the REST API is enabled in Wowza Streaming Engine Manager.
API Authentication:
- Use the REST API username and password for authentication.
Step 2: Create a Stream File
Node.js Code
Install Dependencies:
npm install axios
Code:
const axios = require('axios'); const wowzaApiUrl = 'http://<server-ip>:8087/v2'; const username = 'your-username'; const password = 'your-password'; async function createStreamFile() { const streamFileConfig = { id: "MyStreamFile.stream", uri: "rtmp://live.twitch.tv/app/<stream-key>" // Replace with your stream URI }; try { const response = await axios.post( `${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles`, streamFileConfig, { auth: { username, password }, headers: { 'Content-Type': 'application/json' } } ); console.log('Stream file created:', response.data); } catch (error) { console.error('Error creating stream file:', error.response.data); } } createStreamFile();
Python Code
Install Dependencies:
pip install requests
Code:
import requests from requests.auth import HTTPBasicAuth wowza_api_url = 'http://<server-ip>:8087/v2' username = 'your-username' password = 'your-password' def create_stream_file(): stream_file_config = { "id": "MyStreamFile.stream", "uri": "rtmp://live.twitch.tv/app/<stream-key>" # Replace with your stream URI } url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles" try: response = requests.post( url, json=stream_file_config, auth=HTTPBasicAuth(username, password) ) if response.status_code == 201: print("Stream file created:", response.json()) else: print("Error:", response.status_code, response.json()) except Exception as e: print("Error creating stream file:", e) create_stream_file()
Step 3: Start a Stream File
Node.js Code
async function startStreamFile(streamFileId) {
try {
const response = await axios.put(
`${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/${streamFileId}/actions/connect`,
{},
{
auth: { username, password },
headers: { 'Content-Type': 'application/json' }
}
);
console.log('Stream file started:', response.data);
} catch (error) {
console.error('Error starting stream file:', error.response.data);
}
}
startStreamFile("MyStreamFile.stream");
Python Code
def start_stream_file(stream_file_id):
url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/{stream_file_id}/actions/connect"
try:
response = requests.put(url, auth=HTTPBasicAuth(username, password))
if response.status_code == 200:
print("Stream file started:", response.json())
else:
print("Error:", response.status_code, response.json())
except Exception as e:
print("Error starting stream file:", e)
start_stream_file("MyStreamFile.stream")
Step 4: List All Stream Files
Node.js Code
async function listStreamFiles() {
try {
const response = await axios.get(
`${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles`,
{
auth: { username, password },
headers: { 'Content-Type': 'application/json' }
}
);
console.log('Stream files:', response.data);
} catch (error) {
console.error('Error listing stream files:', error.response.data);
}
}
listStreamFiles();
Python Code
def list_stream_files():
url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles"
try:
response = requests.get(url, auth=HTTPBasicAuth(username, password))
if response.status_code == 200:
print("Stream files:", response.json())
else:
print("Error:", response.status_code, response.json())
except Exception as e:
print("Error listing stream files:", e)
list_stream_files()
Step 5: Delete a Stream File
Node.js Code
async function deleteStreamFile(streamFileId) {
try {
const response = await axios.delete(
`${wowzaApiUrl}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/${streamFileId}`,
{
auth: { username, password },
headers: { 'Content-Type': 'application/json' }
}
);
console.log('Stream file deleted:', response.data);
} catch (error) {
console.error('Error deleting stream file:', error.response.data);
}
}
deleteStreamFile("MyStreamFile.stream");
Python Code
def delete_stream_file(stream_file_id):
url = f"{wowza_api_url}/servers/_defaultServer_/vhosts/_defaultVHost_/applications/LiveStreamApp/streamfiles/{stream_file_id}"
try:
response = requests.delete(url, auth=HTTPBasicAuth(username, password))
if response.status_code == 200:
print("Stream file deleted:", response.json())
else:
print("Error:", response.status_code, response.json())
except Exception as e:
print("Error deleting stream file:", e)
delete_stream_file("MyStreamFile.stream")
Example Use Case
Scenario:
- You want to stream to Twitch using a pre-configured stream file.
Steps:
Use the createStreamFile function to create a stream file pointing to the Twitch RTMP server.
Use the startStreamFile function to start the stream.
List all stream files to verify the connection.
Next Steps:
Automate stream file creation and management.
Integrate stream file management into a custom web interface or monitoring tool.