# ffmpeg To deploy an application using ffmpeg (in our case Python), we need to install this set of programs into the container itself, and not just the python library that is installed via the pip package manager. ## Installation via Dockerfile Using Docker to install FFMpeg. **Steps:** 1. Create a Dockerfile in the project directory. 2. In the Dockerfile we specify the base Python image ```dockerfile FROM python:3.8-slim ``` 3. Run the FFMpeg installation command: ```dockerfile RUN apt-get update && apt-get install -y ffmpeg ``` 4. Copy the project to the image being built and assign WORKDIR: ```dockerfile COPY . /usr/src/app WORKDIR /usr/src/app ``` 5. Add a command to launch the application: ```dockerfile CMD ["python", "main.py"] ``` **The resulting Dockerfile:** ```dockerfile FROM python:3.9 RUN apt-get update && apt-get install -y ffmpeg COPY . /usr/src/app WORKDIR /usr/src/app RUN pip install -r requirements.txt COPY . /usr/src/app CMD ["python", "bot.py"] ``` The created file must be placed in the root of the project. ```{eval-rst} .. admonition:: Note :class: info - Instead of `3.8-slim` you can specify any other Python version you need.. - If you are using a different programming language, you will need to change the FFMpeg installation command to match your language.. ``` **Note:** - Instead of `3.8-slim` it is possible to specify another Python version that is required by the application. - If you use a different programming language, you must select a different base image with the desired language and install the library for that language. ## Example Let's create a telegram bot in python using the telebot and ffmpeg-python libraries, which will accept user videos and add watermarks to them. ```{eval-rst} .. admonition:: Note :class: warning The code is a demo example and we strongly recommend against storing bot tokens or similar data in the code. Use environment variables (secrets)! ``` ```python import os import telebot import ffmpeg bot = telebot.TeleBot('YOUR_BOT_TOKEN') @bot.message_handler(commands=['start']) def handle_start(message): bot.send_message(message.chat.id, "Hi! Send me the video and I will add watermark using Ffmpeg.") @bot.message_handler(content_types=['video']) def handle_video(message): video = message.video file_info = bot.get_file(video.file_id) downloaded_file = bot.download_file(file_info.file_path) with open('video.mp4', 'wb') as new_file: new_file.write(downloaded_file) in_file = ffmpeg.input('video.mp4') overlay_file = ffmpeg.input('watermark.png') ( ffmpeg .concat( in_file.trim(start_frame=10, end_frame=20), in_file.trim(start_frame=30, end_frame=40), ) .overlay(overlay_file.hflip()) .drawbox(50, 50, 120, 120, color='red', thickness=5) .output('out.mp4') .run() ) with open('out.mp4', 'rb') as video: bot.send_video(message.chat.id, video) os.remove('video.mp4') os.remove('out.mp4') bot.polling() ``` **Let's add the following files to the project root:** - yaml configuration file: ```yaml meta: environment: docker toolchain: name: docker version: latest build: dockerfile: Dockerfile run: persistenceMount: /data containerPort: 80 ``` - Dockerfile: ```dockerfile FROM python:3.9 RUN apt-get update && apt-get install -y ffmpeg COPY . /usr/src/app WORKDIR /usr/src/app RUN pip install -r requirements.txt COPY . /usr/src/app CMD ["python", "bot.ru"] ``` - The file with dependencies requirements.txt. In our case, it is pyTelegramBotAPI (the official name of telebot) and ffmpeg-python. ``` pyTelegramBotAPI==4.15.4 ffmpeg-python==0.2.0 ``` ## Possible errors - Python can't find .ru file - check if you have changed the name of **main.ru** in the `CMD` parameter of **Dockerfile** or in the `command` parameter of **amverum.yml** to the name of your script or the path to it. - `AttributeError: module 'ffmpeg' has no attribute 'input'` - make sure you installed **ffmpeg-python** and not **python-ffmpeg**.