commit 9c2a3dc490e5c809cef05800bf6615ab0021a0de Author: Jan Scheiper Date: Sat Feb 13 22:27:03 2021 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e16ca27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +.vscode +constants.py \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8901e34 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Octoprint ffmpeg + +To get started copy `constants.example.py` to `constants.py`: + +``` +$ cp constants.example.py constants.py +``` + +Afterwards make `stream_start.py` and `stream_stop.py` executable: + +``` +$ chmod +x stream_start.py +$ chmod +x stream_stop.py +``` + +You can now start the stream by simply running + +``` +./stream_start.py +``` + +and stop the stream with + +``` +./stream_stop.py +``` \ No newline at end of file diff --git a/constants.example.py b/constants.example.py new file mode 100644 index 0000000..016bc4b --- /dev/null +++ b/constants.example.py @@ -0,0 +1,5 @@ +WEBCAM_STREAM_URL = "" # <-- YOUR WEBCAM URL HERE +TWITCH_STREAM_KEY = "" # <-- YOUR STREAM KEY HERE + +TWITCH_SERVER = f"rtmp://fra02.contribute.live-video.net/app/{TWITCH_STREAM_KEY}" +PIDFILE = "octoprint_ffmpeg.pid" \ No newline at end of file diff --git a/stream_start.py b/stream_start.py new file mode 100755 index 0000000..2cad347 --- /dev/null +++ b/stream_start.py @@ -0,0 +1,24 @@ +#!/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") \ No newline at end of file diff --git a/stream_stop.py b/stream_stop.py new file mode 100755 index 0000000..bc1eee8 --- /dev/null +++ b/stream_stop.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import subprocess, os, signal +from constants import * + +pid_path = os.path.join("/tmp", PIDFILE) + +if os.path.exists(pid_path): + with open(pid_path, "r") as pid_file: + kill_pid = pid_file.read().strip() + + try: + os.kill(int(kill_pid), signal.SIGINT) + + os.unlink(pid_path) + + print("Stopped stream") + except Exception as err: + print("Error occured while killing process:", repr(err)) +else: + print("No pidfile found") +# else do nothing I guess \ No newline at end of file