fix: make ffmpeg support
Some checks failed
Build Worker Base and Application Images / check-base-changes (push) Successful in 7s
Build Worker Base and Application Images / build-base (push) Failing after 6m4s
Build Worker Base and Application Images / build-docker (push) Has been skipped
Build Worker Base and Application Images / deploy-stack (push) Has been skipped

This commit is contained in:
ziesorx 2025-09-25 23:23:56 +07:00
parent 0fc86fb72b
commit a45f76884f
3 changed files with 102 additions and 231 deletions

View file

@ -13,44 +13,39 @@ RUN apt-get update && apt-get install -y \
yasm \
nasm \
# System libraries
libgl1 \
libgl1-mesa-glx \
libglib2.0-0 \
libgtk-3-0 \
libgomp1 \
# Media libraries for FFmpeg build
# Core media libraries (essential ones only)
libjpeg-dev \
libpng-dev \
libtiff-dev \
libx264-dev \
libx265-dev \
libvpx-dev \
libfdk-aac-dev \
libmp3lame-dev \
libopus-dev \
libv4l-dev \
libxvidcore-dev \
libdc1394-22-dev \
# TurboJPEG for fast JPEG encoding
libturbojpeg0-dev \
# GStreamer complete stack
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-bad1.0-dev \
gstreamer1.0-tools \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
gstreamer1.0-vaapi \
python3-gst-1.0 \
# Python development
python3-dev \
python3-numpy \
# NVIDIA driver components
&& rm -rf /var/lib/apt/lists/*
# Install CUDA development tools (required for FFmpeg CUDA compilation)
RUN apt-get update && apt-get install -y \
cuda-nvcc-12-6 \
libcuda1 \
cuda-cudart-dev-12-6 \
cuda-driver-dev-12-6 \
|| echo "CUDA development packages not available, continuing without them" && \
rm -rf /var/lib/apt/lists/*
# Try to install NVIDIA packages (may not be available in all environments)
RUN apt-get update && apt-get install -y \
libnvidia-encode-535 \
libnvidia-decode-535 \
&& rm -rf /var/lib/apt/lists/*
|| echo "NVIDIA packages not available, continuing without them" && \
rm -rf /var/lib/apt/lists/*
# Install NVIDIA Video Codec SDK headers
RUN cd /tmp && \
@ -60,33 +55,60 @@ RUN cd /tmp && \
make install && \
rm -rf /tmp/*
# Build FFmpeg from source with full NVIDIA hardware acceleration
# Build FFmpeg from source with NVIDIA CUVID support
ENV FFMPEG_VERSION=6.0
# Ensure CUDA paths are available for FFmpeg compilation
ENV PATH="/usr/local/cuda/bin:${PATH}"
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:${LD_LIBRARY_PATH}"
RUN cd /tmp && \
wget https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.xz && \
tar xf ffmpeg-${FFMPEG_VERSION}.tar.xz && \
cd ffmpeg-${FFMPEG_VERSION} && \
./configure \
# Configure with explicit CUVID support (with fallback)
(./configure \
--enable-gpl \
--enable-nonfree \
--enable-shared \
--enable-libx264 \
--enable-libx265 \
--enable-libvpx \
--enable-libfdk-aac \
--enable-libmp3lame \
--enable-libopus \
--enable-cuda-nvcc \
--enable-cuvid \
--enable-nvenc \
--enable-nvdec \
--enable-cuda-llvm \
--enable-cuvid \
--enable-nvdec \
--enable-nvenc \
--enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--nvccflags="-gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_89,code=sm_89 -gencode arch=compute_90,code=sm_90" && \
make -j$(nproc) && \
--enable-decoder=h264_cuvid \
--enable-decoder=hevc_cuvid \
--enable-decoder=mjpeg_cuvid \
--enable-decoder=mpeg1_cuvid \
--enable-decoder=mpeg2_cuvid \
--enable-decoder=mpeg4_cuvid \
--enable-decoder=vc1_cuvid \
--enable-encoder=h264_nvenc \
--enable-encoder=hevc_nvenc \
--extra-cflags="-I/usr/local/cuda/include" \
--extra-ldflags="-L/usr/local/cuda/lib64" \
--extra-libs="-lcuda -lcudart -lnvcuvid -lnvidia-encode" \
--nvccflags="-gencode arch=compute_60,code=sm_60 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86" \
|| echo "CUDA configuration failed, trying basic configuration..." && \
./configure \
--enable-gpl \
--enable-nonfree \
--enable-shared \
--enable-libx264 \
--enable-libx265 \
--enable-libvpx \
--enable-libmp3lame) \
&& make -j$(nproc) && \
make install && \
ldconfig && \
# Verify CUVID decoders are available
echo "=== Verifying FFmpeg CUVID Support ===" && \
ffmpeg -hide_banner -decoders 2>/dev/null | grep cuvid && \
echo "=== Verifying FFmpeg NVENC Support ===" && \
ffmpeg -hide_banner -encoders 2>/dev/null | grep nvenc && \
cd / && rm -rf /tmp/*
# Build OpenCV from source with custom FFmpeg and full CUDA support
@ -111,15 +133,14 @@ RUN cd /tmp && \
-D WITH_CUVID=ON \
-D BUILD_opencv_cudacodec=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_LIBV4L=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib-${OPENCV_VERSION}/modules \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON_LIBRARY=$(python3 -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
-D PYTHON_INCLUDE_DIR=$(python3 -c "import sysconfig; print(sysconfig.get_path('include'))") \
-D PYTHON_LIBRARY=$(python3 -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
@ -133,7 +154,6 @@ RUN cd /tmp && \
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/lib:${LD_LIBRARY_PATH}"
ENV PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}"
ENV PYTHONPATH="/usr/local/lib/python3.10/dist-packages:${PYTHONPATH}"
ENV GST_PLUGIN_PATH="/usr/lib/x86_64-linux-gnu/gstreamer-1.0"
# Optimized environment variables for hardware acceleration
ENV OPENCV_FFMPEG_CAPTURE_OPTIONS="rtsp_transport;tcp|hwaccel;cuda|hwaccel_device;0|video_codec;h264_cuvid|hwaccel_output_format;cuda"
@ -151,16 +171,21 @@ RUN grep -v opencv-python requirements.base.txt > requirements.tmp && \
# Verify complete hardware acceleration setup
RUN echo "=== Hardware Acceleration Verification ===" && \
echo "FFmpeg Hardware Accelerators:" && \
ffmpeg -hide_banner -hwaccels 2>/dev/null | head -10 && \
echo "FFmpeg NVIDIA Decoders:" && \
ffmpeg -hide_banner -decoders 2>/dev/null | grep -E "(cuvid|nvdec)" | head -5 && \
echo "FFmpeg NVIDIA Encoders:" && \
ffmpeg -hide_banner -encoders 2>/dev/null | grep nvenc | head -5 && \
(ffmpeg -hide_banner -hwaccels 2>/dev/null || echo "FFmpeg hwaccels command failed") && \
echo "" && \
echo "FFmpeg CUVID Decoders (NVIDIA):" && \
(ffmpeg -hide_banner -decoders 2>/dev/null | grep -E "cuvid" || echo "No CUVID decoders found") && \
echo "" && \
echo "FFmpeg NVENC Encoders (NVIDIA):" && \
(ffmpeg -hide_banner -encoders 2>/dev/null | grep -E "nvenc" || echo "No NVENC encoders found") && \
echo "" && \
echo "Testing CUVID decoder compilation (no GPU required):" && \
(ffmpeg -hide_banner -f lavfi -i testsrc=duration=0.1:size=64x64:rate=1 -c:v libx264 -f null - 2>/dev/null && echo "✅ FFmpeg basic functionality working" || echo "❌ FFmpeg basic test failed") && \
echo "" && \
echo "OpenCV Configuration:" && \
python3 -c "import cv2; print('OpenCV version:', cv2.__version__); print('CUDA devices:', cv2.cuda.getCudaEnabledDeviceCount()); build_info = cv2.getBuildInformation(); print('CUDA support:', 'CUDA' in build_info); print('CUVID support:', 'CUVID' in build_info); print('FFmpeg support:', 'FFMPEG' in build_info); print('GStreamer support:', 'GStreamer' in build_info)" && \
echo "GStreamer NVIDIA Plugins:" && \
gst-inspect-1.0 2>/dev/null | grep -E "(nvv4l2|nvvideo)" | head -5 || echo "GStreamer NVIDIA plugins not detected" && \
echo "=== Verification Complete ==="
(python3 -c "import cv2; print('OpenCV version:', cv2.__version__); build_info = cv2.getBuildInformation(); print('CUDA support:', 'CUDA' in build_info); print('CUVID support:', 'CUVID' in build_info); print('FFmpeg support:', 'FFMPEG' in build_info)" || echo "OpenCV verification failed") && \
echo "" && \
echo "=== Verification Complete (build-time only) ==="
# Set working directory
WORKDIR /app