|
import os |
|
import random |
|
import subprocess |
|
from multiprocessing import Process |
|
import uvicorn |
|
from fastapi import FastAPI |
|
|
|
app = FastAPI() |
|
|
|
video_directory = "/app" |
|
rtmp_url = "rtmp://live.twitch.tv/app/live_1104664009_xoiSdq7ayaMdFKU4xaCzkCqLw3tYvz" |
|
ffmpeg_command = 'ffmpeg -re -i "{input}" -c:v libx264 -b:v 5000.00k -c:a aac -b:a 128.00k -preset ultrafast -f flv "{output}"' |
|
|
|
def get_video_list(): |
|
return [f for f in os.listdir(video_directory) if f.endswith('.mp4')] |
|
|
|
def stream_video(): |
|
videos = get_video_list() |
|
random.shuffle(videos) |
|
while True: |
|
for video in videos: |
|
video_path = os.path.join(video_directory, video) |
|
command = ffmpeg_command.format(input=video_path, output=rtmp_url) |
|
process = subprocess.Popen(command, shell=True) |
|
process.wait() |
|
random.shuffle(videos) |
|
|
|
def start_video_streaming(): |
|
video_process = Process(target=stream_video) |
|
video_process.start() |
|
|
|
@app.get("/") |
|
async def read_root(): |
|
return {"message": "Hello World"} |
|
|
|
if __name__ == "__main__": |
|
start_video_streaming() |
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|