initial commit

This commit is contained in:
Jan Scheiper 2021-02-13 22:27:03 +01:00
commit 9c2a3dc490
5 changed files with 80 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
__pycache__
.vscode
constants.py

26
README.md Normal file
View File

@ -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
```

5
constants.example.py Normal file
View File

@ -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"

24
stream_start.py Executable file
View File

@ -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")

22
stream_stop.py Executable file
View File

@ -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