I want to compress the video from project directory using ffmpeg in python
the video is saved from cv2.VideoCapture(rtsp_url)
Normally it run without problem in my local machine, but when I dockerize my app it seems docker container can’t recognize ffmpeg or I missed something.
JavaScript
x
11
11
1
def compress(name):
2
with open(name) as f:
3
output = name[0:-4] + "-f"+ ".mp4"
4
input = name
5
subprocess.run('ffmpeg -i ' + input + ' -vcodec libx264 ' + output)
6
7
video = cv2.VideoCapture(rtsp_url) # This is where the video comming from
8
fileName = saveToProjDir(video) # Save the video to project directory and return the name
9
compress(fileName) # Compress the video
10
11
It throws exception
JavaScript
1
18
18
1
Exception in thread Thread-8 (compress):
2
Traceback (most recent call last):
3
File "/usr/local/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
4
self.run()
5
File "/usr/local/lib/python3.11/threading.py", line 975, in run
6
self._target(*self._args, **self._kwargs)
7
File "/app/main.py", line 59, in compress
8
subprocess.run('ffmpeg -i ' + input + ' -vcodec libx264 ' + output)
9
File "/usr/local/lib/python3.11/subprocess.py", line 546, in run
10
with Popen(*popenargs, **kwargs) as process:
11
^^^^^^^^^^^^^^^^^^^^^^^^^^^
12
File "/usr/local/lib/python3.11/subprocess.py", line 1022, in __init__
13
self._execute_child(args, executable, preexec_fn, close_fds,
14
File "/usr/local/lib/python3.11/subprocess.py", line 1899, in _execute_child
15
raise child_exception_type(errno_num, err_msg, err_filename)
16
FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg -i cam1_2022-11-15125845.avi -vcodec libx264 cam1_2022-11-15125845-f.mp4'
17
18
This is how I docker my python app.
JavaScript
1
18
18
1
FROM python:3.11.0
2
3
WORKDIR /app/
4
5
ENV VIRTUAL_ENV = /env
6
RUN python3 -m venv $VIRTUAL_ENV
7
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
8
9
COPY requirements.txt .
10
11
RUN pip install -r requirements.txt
12
RUN apt-get -y update
13
RUN apt-get install ffmpeg libsm6 libxext6 -y
14
15
ADD main.py .
16
CMD ["python","/app/main.py"]
17
18
Advertisement
Answer
To debug your problem my suggestion is to use your container interactively.
Try running a new container like this:
JavaScript
1
2
1
docker run -t -i <image-name-or-container-id> /bin/bash
2
Or attach to your running container:
JavaScript
1
2
1
docker exec -i -t <container-id> /bin/bash
2
In this way you can play around, try to launch ffmpeg from different paths, install other dependencies and finally see what you are missing.