Automating Screenshot Capture and Upload with FFMPEG & Python

Introduction

Capturing and uploading screenshots automatically can be useful for monitoring, streaming, or documentation. This guide will show you how to use FFMPEG and Python to take periodic screenshots and upload them to a remote server.

Step 1: Install Required Packages

  1. Update your package list and install FFMPEG:

    sudo apt update && sudo apt install ffmpeg -y

  2. Install Python and the required libraries:

    sudo apt install python3 python3-pip -y

    pip3 install paramiko


Step 2: Capture Screenshots with FFMPEG

  1. Run the following command to capture a screenshot every 10 seconds:

    ffmpeg -video_size 1920x1080 -framerate 1/10 -f x11grab -i :0.0 screenshot_%04d.png

  2. Adjust the resolution and frame rate as needed.


Step 3: Create a Python Script for Automated Uploads

  1. Create a Python script to upload screenshots:

    sudo nano /usr/local/bin/upload_screenshots.py

  2. Example script:

    import os

    import paramiko

    REMOTE_HOST = "your.server.com"

    REMOTE_USER = "your-user"

    REMOTE_PATH = "/path/to/upload/"

    SSH_KEY = "~/.ssh/id_rsa"

    ssh = paramiko.SSHClient()

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh.connect(REMOTE_HOST, username=REMOTE_USER, key_filename=SSH_KEY)

    sftp = ssh.open_sftp()

    for file in os.listdir("."):

    if file.startswith("screenshot_") and file.endswith(".png"):

    sftp.put(file, REMOTE_PATH + file)

    os.remove(file)

    sftp.close()

    ssh.close()

  3. Save and close the file.


Step 4: Automate the Process with Cron

  1. Open the crontab editor:

    crontab -e

  2. Add the following lines to schedule the tasks:

    */10 * * * * ffmpeg -video_size 1920x1080 -framerate 1/10 -f x11grab -i :0.0 ~/screenshots/screenshot_$(date +%Y%m%d%H%M%S).png

    */15 * * * * python3 /usr/local/bin/upload_screenshots.py

  3. Save and exit.


Conclusion

With this setup, your system will automatically capture screenshots and upload them to a remote server at regular intervals. You can customize the capture frequency, resolution, and upload location to fit your needs.