01:47:04 am 07/29/2023
Viewed: 4846
This script uses three instances of FFmpeg to achieve uninterrupted HLS streaming by switching between a local MP4 file and a RTMP feed.
# Define paths and URLs
$localMP4File = "C:\path\to\local_mp4_file.mp4"
$hlsOutputFolder = "C:\path\to\hls_output"
$hlsLocalPlaylist = "$hlsOutputFolder\index.m3u8"
$hlsTempPlaylist = "$hlsOutputFolder\temp.m3u8"
$hlsFinalPlaylist = "$hlsOutputFolder\final.m3u8"
$rtmpURL = "rtmp://www.tvbydemand.com/live/mixedmedia2"
# Function to start FFmpeg process
function Start-FFmpeg {
param (
[string]$input,
[string]$output
)
Start-Process ffmpeg.exe -ArgumentList "-i $input -c:v copy -c:a copy -f hls $output" -NoNewWindow
}
# Function to stop FFmpeg process
function Stop-FFmpeg {
param (
[int]$processId
)
Stop-Process -Id $processId
}
# Start FFmpeg to convert local MP4 to HLS
Start-FFmpeg -input $localMP4File -output $hlsLocalPlaylist
# Start FFmpeg to receive RTMP and create temp HLS playlist
$rtmpFFmpegProcess = Start-FFmpeg -input $rtmpURL -output $hlsTempPlaylist
# Loop to continuously monitor RTMP availability
while ($true) {
# Check if RTMP feed is available
$rtmpAvailable = Test-NetConnection -ComputerName "www.tvbydemand.com" -Port 1935 -InformationLevel "Quiet"
# If RTMP feed is available, stop the temp FFmpeg process and start the final FFmpeg process
if ($rtmpAvailable) {
Stop-FFmpeg -processId $rtmpFFmpegProcess.Id
Start-FFmpeg -input $rtmpURL -output $hlsFinalPlaylist
break
}
# Wait for a few seconds before checking again
Start-Sleep -Seconds 5
}
In this PowerShell script:
Replace the placeholders C:\path\to\local_mp4_file.mp4 and C:\path\to\hls_output with the actual paths where your local MP4 file is located, and where you want the HLS output to be stored, respectively.
The Start-FFmpeg function takes an input and output argument and starts FFmpeg to convert the input stream to HLS using the provided output path.
The Stop-FFmpeg function takes a process ID argument and stops the FFmpeg process with the corresponding process ID.
The script starts FFmpeg to convert the local MP4 file to HLS and listens for the RTMP feed using two separate instances of FFmpeg. It continuously monitors the availability of the RTMP feed using Test-NetConnection.
When the RTMP feed becomes available, the script stops the temp FFmpeg process and starts the final FFmpeg process, which continuously merges the HLS outputs from the local MP4 file and the RTMP feed into a single HLS stream without re-encoding.
The script runs in an infinite loop, continuously checking for the RTMP feed's availability. If the RTMP feed goes down temporarily, the script will automatically switch back to the local MP4 file without interruption.
# Define paths and URLs
$localMP4File = "C:\path\to\local_mp4_file.mp4"
$hlsOutputFolder = "C:\path\to\hls_output"
$hlsLocalPlaylist = "$hlsOutputFolder\index.m3u8"
$hlsTempPlaylist = "$hlsOutputFolder\temp.m3u8"
$hlsFinalPlaylist = "$hlsOutputFolder\final.m3u8"
$rtmpURL = "rtmp://www.tvbydemand.com/live/mixedmedia2"
# Function to start FFmpeg process
function Start-FFmpeg {
param (
[string]$input,
[string]$output
)
Start-Process ffmpeg.exe -ArgumentList "-i $input -c:v copy -c:a copy -f hls $output" -NoNewWindow
}
# Function to stop FFmpeg process
function Stop-FFmpeg {
param (
[int]$processId
)
Stop-Process -Id $processId
}
# Start FFmpeg to convert local MP4 to HLS
Start-FFmpeg -input $localMP4File -output $hlsLocalPlaylist
# Start FFmpeg to receive RTMP and create temp HLS playlist
$rtmpFFmpegProcess = Start-FFmpeg -input $rtmpURL -output $hlsTempPlaylist
# Loop to continuously monitor RTMP availability
while ($true) {
# Check if RTMP feed is available
$rtmpAvailable = Test-NetConnection -ComputerName "www.tvbydemand.com" -Port 1935 -InformationLevel "Quiet"
# If RTMP feed is available, stop the temp FFmpeg process and start the final FFmpeg process
if ($rtmpAvailable) {
Stop-FFmpeg -processId $rtmpFFmpegProcess.Id
Start-FFmpeg -input $rtmpURL -output $hlsFinalPlaylist
break
}
# Wait for a few seconds before checking again
Start-Sleep -Seconds 5
}
In this PowerShell script:
Replace the placeholders C:\path\to\local_mp4_file.mp4 and C:\path\to\hls_output with the actual paths where your local MP4 file is located, and where you want the HLS output to be stored, respectively.
The Start-FFmpeg function takes an input and output argument and starts FFmpeg to convert the input stream to HLS using the provided output path.
The Stop-FFmpeg function takes a process ID argument and stops the FFmpeg process with the corresponding process ID.
The script starts FFmpeg to convert the local MP4 file to HLS and listens for the RTMP feed using two separate instances of FFmpeg. It continuously monitors the availability of the RTMP feed using Test-NetConnection.
When the RTMP feed becomes available, the script stops the temp FFmpeg process and starts the final FFmpeg process, which continuously merges the HLS outputs from the local MP4 file and the RTMP feed into a single HLS stream without re-encoding.
The script runs in an infinite loop, continuously checking for the RTMP feed's availability. If the RTMP feed goes down temporarily, the script will automatically switch back to the local MP4 file without interruption.
No video exists.
Comments