@echo off cd /d E:\IPTV_SERVER\icons title IPTV Stream Controller setlocal enabledelayedexpansion rem =========================================== rem CONFIGURATION rem =========================================== set START_TIME=07:00 set END_TIME=19:00 rem =========================================== echo **************************************** echo IPTV Stream Scheduler Started echo Stream Time: %START_TIME% to %END_TIME% echo Press Ctrl+C to stop this script echo **************************************** :MAIN_LOOP rem Get current time in HH:MM format for /f "tokens=1-2 delims=:" %%a in ('powershell Get-Date -Format "HH:mm"') do ( set CURRENT_HOUR=%%a set CURRENT_MINUTE=%%b ) set CURRENT_TIME=!CURRENT_HOUR!:!CURRENT_MINUTE! echo [%time%] Current: !CURRENT_TIME! - Schedule: %START_TIME% to %END_TIME% if "!CURRENT_TIME!" geq "%START_TIME%" ( if "!CURRENT_TIME!" lss "%END_TIME%" ( rem --- WITHIN STREAMING HOURS --- echo [%time%] *** STREAM SHOULD BE RUNNING *** tasklist /fi "imagename eq ffmpeg.exe" | find /i "ffmpeg.exe" > nul if errorlevel 1 ( echo [%time%] Launching FFmpeg in separate window... start "FFmpeg Stream" ffmpeg -stream_loop -1 -re -i "Al Jadida.mp4" -c:v libx264 -c:a aac -f hls -hls_time 10 -hls_list_size 5 -hls_flags delete_segments -hls_segment_filename "segment_%%03d.ts" -hls_base_url "http://khalil1342.ddns.net:34401/" output.m3u8 echo [%time%] FFmpeg stream started successfully! ) else ( echo [%time%] FFmpeg is already running ) rem Wait 1 minute and check if we're still within streaming hours echo [%time%] Monitoring stream... (next check in 1 minute) timeout /t 60 /nobreak > nul rem Check current time again to see if we should continue streaming for /f "tokens=1-2 delims=:" %%a in ('powershell Get-Date -Format "HH:mm"') do ( set CURRENT_HOUR=%%a set CURRENT_MINUTE=%%b ) set CURRENT_TIME=!CURRENT_HOUR!:!CURRENT_MINUTE! if "!CURRENT_TIME!" lss "%END_TIME%" ( echo [%time%] Still within streaming hours, continuing... goto MAIN_LOOP ) else ( echo [%time%] *** END TIME REACHED - STOPPING STREAM *** taskkill /f /im ffmpeg.exe > nul 2>&1 echo [%time%] FFmpeg stopped successfully! call :CleanupSegments goto MAIN_LOOP ) ) else ( rem This handles the case where current time is past END_TIME but still checking the first condition echo [%time%] *** END TIME PASSED - STOPPING STREAM *** taskkill /f /im ffmpeg.exe > nul 2>&1 echo [%time%] FFmpeg stopped! call :CleanupSegments ) ) rem --- OUTSIDE STREAMING HOURS --- echo [%time%] *** STREAM SHOULD BE STOPPED *** tasklist /fi "imagename eq ffmpeg.exe" | find /i "ffmpeg.exe" > nul if not errorlevel 1 ( echo [%time%] Stopping FFmpeg stream... taskkill /f /im ffmpeg.exe > nul 2>&1 echo [%time%] FFmpeg stopped successfully! call :CleanupSegments ) else ( echo [%time%] FFmpeg is not running ) rem Wait 5 minutes and check again echo [%time%] Waiting for stream time... (next check in 1 minute) timeout /t 60 /nobreak > nul goto MAIN_LOOP rem =========================================== rem SUBROUTINE: Clean up HLS segments and playlist rem =========================================== :CleanupSegments echo [%time%] Cleaning up leftover segments and files... del "segment_*.ts" > nul 2>&1 if errorlevel 1 ( echo [%time%] No segment files found to delete ) else ( echo [%time%] Segment files deleted successfully ) del "output.m3u8" > nul 2>&1 if errorlevel 1 ( echo [%time%] No playlist file found to delete ) else ( echo [%time%] Playlist file deleted successfully ) rem Also clean up any temporary files FFmpeg might have created del "ffmpeg*.tmp" > nul 2>&1 del "*.tmp" > nul 2>&1 echo [%time%] Cleanup completed! goto :eof