Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeanon committed Jan 19, 2025
2 parents f38db80 + e5ff187 commit 07c5d4d
Show file tree
Hide file tree
Showing 25 changed files with 1,790 additions and 1,055 deletions.
51 changes: 39 additions & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,44 @@ jobs:
name: Pre-commit
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10']
runs-on: ubuntu-latest
python-version: ['3.9', '3.10', '3.11', '3.12']
runs-on: ubuntu-22.04
steps:
- name: Check out the repository
uses: actions/checkout@v3.3.0
uses: actions/checkout@v4.1.1

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4.5.0
uses: actions/setup-python@v5.2.0
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: pip install -r scripts/requirements.dev.txt
run: |
sudo apt install ffmpeg -y
pip install -r scripts/requirements.dev.txt
- name: Run pre-commit on all files
run: |
pre-commit run --all-files --show-diff-on-failure --color=always
- name: Test with pytest
run: |
pytest -v
docker:
needs: pre-commit
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2.1.0
uses: docker/setup-qemu-action@v3.2.0
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2.2.1
uses: docker/setup-buildx-action@v3.6.1
-
name: Docker meta
id: meta
uses: docker/metadata-action@v4.2.0
uses: docker/metadata-action@v5.5.1
with:
images: lefskiy/moonraker-telegram-bot
tags: |
Expand All @@ -55,17 +58,41 @@ jobs:
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
-
name: Docker meta
id: meta_mjpeg
uses: docker/[email protected]
with:
images: lefskiy/moonraker-telegram-bot
flavor: |
prefix=mjpeg-,onlatest=true
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix=sha-mjpeg-
-
name: Login to Docker Hub
uses: docker/login-action@v2.1.0
uses: docker/login-action@v3.3.0
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v3.2.0
uses: docker/build-push-action@v6.7.0
with:
file: Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7
platforms: linux/amd64,linux/arm64
-
name: Build and push mjpeg
uses: docker/[email protected]
with:
file: Dockerfile-mjpeg
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta_mjpeg.outputs.tags }}
labels: ${{ steps.meta_mjpeg.outputs.labels }}
platforms: linux/amd64,linux/arm64
18 changes: 8 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
FROM python:3.9-slim-bullseye
FROM python:3.12.7-slim-bookworm

RUN apt update \
&& apt install -y \
python3-dev python3-setuptools python3-virtualenv \
python3-cryptography python3-gevent python3-opencv \
x264 libx264-dev libwebp-dev \
libtiff5-dev libjpeg-dev libopenjp2-7-dev zlib1g-dev \
libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python3-tk \
libharfbuzz-dev libfribidi-dev libxcb1-dev \
ffmpeg x264 libx264-dev libuv1 \
libwebp-dev libtiff5-dev libjpeg*-turbo libjpeg*-turbo-dev libopenjp2-7-dev \
zlib1g-dev libfreetype6-dev liblcms2-dev \
&& apt clean \
&& rm -rf /var/lib/apt/lists/*


WORKDIR /opt

RUN groupadd moonraker-telegram-bot --gid 1000 \
Expand All @@ -19,10 +17,10 @@ RUN groupadd moonraker-telegram-bot --gid 1000 \
&& chown -R moonraker-telegram-bot:moonraker-telegram-bot /opt/*

COPY --chown=moonraker-telegram-bot:moonraker-telegram-bot . ./moonraker-telegram-bot
RUN virtualenv -p /usr/bin/python3 --system-site-packages venv \
&& venv/bin/pip install --no-use-pep517 --no-cache-dir -r moonraker-telegram-bot/scripts/requirements.txt
RUN pip3 install --no-cache-dir -r moonraker-telegram-bot/scripts/requirements.docker.opencv.txt


USER moonraker-telegram-bot
VOLUME [ "/opt/printer_data/logs", "/opt/printer_data/config", "/opt/timelapse","/opt/timelapse_finished"]
ENTRYPOINT ["/opt/venv/bin/python3", "moonraker-telegram-bot/bot/main.py"]
ENTRYPOINT ["python3", "moonraker-telegram-bot/bot/main.py"]
CMD ["-c", "/opt/printer_data/config/telegram.conf", "-l", "/opt/printer_data/logs"]
30 changes: 30 additions & 0 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM python:3.12.7-bookworm

RUN apt update \
&& apt install -y \
ffmpeg x264 libx264-dev libuv1 \
libwebp-dev libtiff5-dev libjpeg*-turbo libjpeg*-turbo-dev libopenjp2-7-dev \
zlib1g-dev libfreetype6-dev liblcms2-dev \
build-essential python3-dev libdebuginfod-dev libunwind-dev liblz4-dev \
&& apt clean \
&& rm -rf /var/lib/apt/lists/*


WORKDIR /opt

RUN groupadd moonraker-telegram-bot --gid 1000 \
&& useradd moonraker-telegram-bot --uid 1000 --gid moonraker-telegram-bot \
&& mkdir -p printer_data/logs printer_data/config timelapse timelapse_finished \
&& chown -R moonraker-telegram-bot:moonraker-telegram-bot /opt/*

COPY --chown=moonraker-telegram-bot:moonraker-telegram-bot . ./moonraker-telegram-bot
RUN pip3 install --no-cache-dir -r moonraker-telegram-bot/scripts/requirements.docker.opencv.txt

RUN pip3 install --no-cache-dir memray==1.14.0
RUN pip3 install --no-cache-dir memory-profiler

USER moonraker-telegram-bot
VOLUME [ "/opt/printer_data/logs", "/opt/printer_data/config", "/opt/timelapse","/opt/timelapse_finished"]
# ENTRYPOINT ["python3", "-m", "memray", "run", "--native", "--trace-python-allocators", "moonraker-telegram-bot/bot/main.py"]
ENTRYPOINT ["python3", "moonraker-telegram-bot/bot/main.py"]
CMD ["-c", "/opt/printer_data/config/telegram.conf", "-l", "/opt/printer_data/logs"]
25 changes: 25 additions & 0 deletions Dockerfile-mjpeg
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM python:3.12.7-slim-bookworm

RUN apt update \
&& apt install -y \
ffmpeg x264 libx264-dev libuv1 \
libwebp-dev libtiff5-dev libjpeg*-turbo libjpeg*-turbo-dev libopenjp2-7-dev \
zlib1g-dev libfreetype6-dev liblcms2-dev \
&& apt clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /opt

RUN groupadd moonraker-telegram-bot --gid 1000 \
&& useradd moonraker-telegram-bot --uid 1000 --gid moonraker-telegram-bot \
&& mkdir -p printer_data/logs printer_data/config timelapse timelapse_finished \
&& chown -R moonraker-telegram-bot:moonraker-telegram-bot /opt/*

COPY --chown=moonraker-telegram-bot:moonraker-telegram-bot . ./moonraker-telegram-bot
RUN pip3 install --no-cache-dir -r moonraker-telegram-bot/scripts/requirements.docker.mjpeg.txt


USER moonraker-telegram-bot
VOLUME [ "/opt/printer_data/logs", "/opt/printer_data/config", "/opt/timelapse","/opt/timelapse_finished"]
ENTRYPOINT ["python3", "moonraker-telegram-bot/bot/main.py"]
CMD ["-c", "/opt/printer_data/config/telegram.conf", "-l", "/opt/printer_data/logs"]
47 changes: 47 additions & 0 deletions bot/assets/ffmpegcv_custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import logging

from ffmpegcv.ffmpeg_reader import FFmpegReader, get_outnumpyshape, get_videofilter_cpu # type: ignore
from ffmpegcv.stream_info import get_info # type: ignore

logger = logging.getLogger(__name__)


class FFmpegReaderStreamRTCustom(FFmpegReader):
def __init__(self):
super().__init__()

@staticmethod
def VideoReader(stream_url, codec, pix_fmt, crop_xywh, resize, resize_keepratio, resize_keepratioalign, timeout, videoinfo):
vid = FFmpegReaderStreamRTCustom()
videoinfo = videoinfo if videoinfo else get_info(stream_url, timeout)
vid.origin_width = videoinfo.width
vid.origin_height = videoinfo.height
vid.fps = videoinfo.fps
vid.codec = codec if codec else videoinfo.codec
vid.count = videoinfo.count
vid.duration = videoinfo.duration
vid.pix_fmt = pix_fmt

(vid.crop_width, vid.crop_height), (vid.width, vid.height), filteropt = get_videofilter_cpu(
(vid.origin_width, vid.origin_height), pix_fmt, crop_xywh, resize, resize_keepratio, resize_keepratioalign
)
vid.size = (vid.width, vid.height)

rtsp_opt = "-rtsp_transport tcp " if stream_url.startswith("rtsp://") else ""
vid.ffmpeg_cmd = (
f"ffmpeg -loglevel warning "
f" {rtsp_opt} "
" -probesize 32 -analyzeduration 0 -fflags discardcorrupt "
"-fflags nobuffer -flags low_delay -strict experimental "
f" -vcodec {vid.codec} -i {stream_url}"
f" {filteropt} -pix_fmt {pix_fmt} -f rawvideo pipe:"
)

vid.out_numpy_shape = get_outnumpyshape(vid.size, pix_fmt)
return vid


def FFmpegReaderStreamRTCustomInit(
stream_url, codec=None, pix_fmt="bgr24", crop_xywh=None, resize=None, resize_keepratio=True, resize_keepratioalign="center", timeout=None, videoinfo=None
) -> FFmpegReaderStreamRTCustom:
return FFmpegReaderStreamRTCustom.VideoReader(stream_url, codec, pix_fmt, crop_xywh, resize, resize_keepratio, resize_keepratioalign, timeout=timeout, videoinfo=videoinfo)
Loading

0 comments on commit 07c5d4d

Please sign in to comment.