octoprint_ffmpeg_script/stream_start.py
2021-02-13 22:27:03 +01:00

24 lines
868 B
Python
Executable File

#!/usr/bin/env python3
import subprocess, os, sys
from constants import *
pid_path = os.path.join("/tmp", PIDFILE)
if os.path.exists(pid_path):
with open(pid_path, "r") as pid_file:
check_pid = pid_file.read().strip()
try:
os.kill(int(check_pid), 0)
print(f"ffmpeg is already running. Delete {pid_path} if it actually isn't.")
sys.exit(0) # apparently ffmpeg is running, just quit and do nothing
except OSError:
pass
proc = subprocess.Popen([ "/usr/bin/ffmpeg", "-i", WEBCAM_STREAM_URL, "-video_size", "1280x720", "-an", "-f", "flv", "-c:v", "libx264", "-b:v", "500k", "-preset", "veryfast", "-bufsize", "1M", "-maxrate", "500k", TWITCH_SERVER ], stderr=open(os.devnull, "w"))
# write pid into pidfile so we can kill the process later
with open(pid_path, "w") as pid_file:
pid_file.write(str(proc.pid))
print(f"Wrote PID {proc.pid} into pidfile")