In-memory video processing library powered by FFmpeg
Project description
pymedia
In-memory video processing library for Python, powered by FFmpeg. No temporary files, no subprocesses — everything runs in-process via ctypes.
pip install python-media
from pymedia import extract_audio, get_video_info, trim_video
Features
- Extract audio — pull audio from video as mp3, wav, aac, or ogg
- Video info — get duration, resolution, codecs, fps, bitrate, etc.
- Convert format — remux to mp4, mkv, webm, avi, mov (fast, no re-encoding)
- Trim video — cut a time segment
- Mute video — strip all audio tracks
- Compress video — re-encode with H.264 at a target quality (CRF)
- Resize video — change resolution
- Extract frame — grab a single frame as JPEG or PNG
- Video to GIF — convert video (or a segment) to animated GIF
Installation
Quick install (recommended)
The install script auto-detects your OS, installs FFmpeg dev libraries, and installs pymedia — all in one command:
git clone https://github.com/moinakmalkhan/pymedia.git
cd pymedia
./install.sh
Manual install
If you prefer to install step by step, first install the system dependencies for your platform, then install pymedia.
Step 1: Install system dependencies
Ubuntu / Debian / Linux Mint / Pop!_OS
sudo apt update
sudo apt install gcc pkg-config \
libavformat-dev libavcodec-dev libavutil-dev \
libswresample-dev libswscale-dev
Fedora
sudo dnf install gcc pkg-config \
ffmpeg-free-devel libavcodec-free-devel libavformat-free-devel \
libavutil-free-devel libswresample-free-devel libswscale-free-devel
Or with RPM Fusion enabled (for full codec support):
sudo dnf install gcc pkg-config ffmpeg-devel
CentOS / RHEL / Rocky / AlmaLinux
sudo dnf install gcc pkg-config ffmpeg-devel
Arch Linux / Manjaro / EndeavourOS
sudo pacman -S gcc pkg-config ffmpeg
openSUSE
sudo zypper install gcc pkg-config ffmpeg-devel
macOS (Homebrew)
brew install gcc pkg-config ffmpeg
Windows (via WSL)
pymedia does not support Windows natively. Use WSL (Windows Subsystem for Linux):
wsl --install
Then inside WSL, follow the Ubuntu/Debian instructions above.
Step 2: Install pymedia
The C library is compiled automatically during pip install — no need to run make manually:
git clone https://github.com/moinakmalkhan/pymedia.git
cd pymedia
pip install .
For development (editable install):
git clone https://github.com/moinakmalkhan/pymedia.git
cd pymedia
python3 -m venv .venv
source .venv/bin/activate
pip install -e .
Verify installation
python -c "from pymedia import get_video_info; print('pymedia installed successfully')"
Usage
from pymedia import (
extract_audio, get_video_info, convert_format,
trim_video, mute_video, compress_video, resize_video,
extract_frame, video_to_gif,
)
with open("video.mp4", "rb") as f:
data = f.read()
# Get video metadata
info = get_video_info(data)
print(info["duration"], info["width"], info["height"])
# Extract audio as mp3
mp3 = extract_audio(data, format="mp3")
# Convert to webm
webm = convert_format(data, format="webm")
# Trim first 10 seconds
clip = trim_video(data, start=0, end=10)
# Remove audio
silent = mute_video(data)
# Compress (lower CRF = better quality)
small = compress_video(data, crf=28, preset="fast")
# Resize to 720p width
resized = resize_video(data, width=1280)
# Extract a frame at 5 seconds as JPEG
frame = extract_frame(data, timestamp=5.0, format="jpeg")
# Convert to GIF (320px wide, 10fps, first 3 seconds)
gif = video_to_gif(data, width=320, fps=10, start=0, duration=3)
Supported formats
| Function | Formats |
|---|---|
extract_audio |
mp3, wav, aac, ogg |
convert_format |
mp4, mkv, webm, avi, mov, flv, ts |
extract_frame |
jpeg, png |
compress_video / resize_video |
H.264 mp4 output |
video_to_gif |
GIF |
Platform support
| Platform | Status |
|---|---|
| Linux (x86_64) | Fully supported |
| Linux (ARM64) | Supported (build from source) |
| macOS (Homebrew) | Supported (build from source) |
| Windows (WSL) | Supported via WSL |
| Windows (native) | Not supported |
Contributing
Contributions are welcome! pymedia is open source and we appreciate help from the community.
Setting up the development environment
- Fork the repo on GitHub and clone your fork:
git clone https://github.com/<your-username>/pymedia.git
cd pymedia
-
Install system dependencies (see Installation for your platform).
-
Create a virtual environment and install in dev mode:
python3 -m venv .venv
source .venv/bin/activate
pip install -e .
pip install pytest
- Run the tests to make sure everything works:
pytest tests/ -v
Making changes
- Create a branch for your change:
git checkout -b my-feature
-
Make your changes. If you're adding a new feature:
- Add the C function in
src/pymedia/_lib/pymedia.c - Add ctypes bindings in
src/pymedia/_core.py - Add the Python wrapper in the appropriate module (
audio.py,video.py,frames.py, orinfo.py) - Export it from
src/pymedia/__init__.py - Add tests in
tests/
- Add the C function in
-
Rebuild after any C changes:
pip install -e .
- Run the tests:
pytest tests/ -v
- Commit and push:
git add <files>
git commit -m "Short description of the change"
git push origin my-feature
- Open a Pull Request on GitHub.
Project structure
src/pymedia/
├── __init__.py # Public API exports
├── _core.py # ctypes bindings (loads libpymedia.so)
├── audio.py # extract_audio
├── video.py # convert_format, compress, resize, trim, mute, to_gif
├── info.py # get_video_info
├── frames.py # extract_frame
└── _lib/
├── pymedia.c # All C code (FFmpeg operations)
└── libpymedia.so # Built automatically by `pip install` (not committed to git)
Guidelines
- Keep Python wrappers thin — heavy lifting goes in the C code
- Every new feature needs at least one test
- Tests must not require external files — generate test data in
tests/conftest.py - Run
pytest tests/ -vbefore submitting a PR and make sure all tests pass
Ideas for contributions
- Add new video operations (watermark, rotate, change speed, reverse, merge)
- Improve GIF quality (palette generation)
- Add Windows native support
- Migrate deprecated FFmpeg API calls to the new channel layout API
- Expand CI/CD pipeline (test matrix, pre-built wheels)
- Improve error messages from the C layer
Reporting bugs
Open an issue at https://github.com/moinakmalkhan/pymedia/issues with:
- What you did
- What you expected
- What happened instead
- Your OS and FFmpeg version (
ffmpeg -version)
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_media-0.1.0.tar.gz.
File metadata
- Download URL: python_media-0.1.0.tar.gz
- Upload date:
- Size: 34.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da0e9f329ac1880d1bd5b9d217c0b628e33691317e171c7575469c1c28422345
|
|
| MD5 |
5d2261322e9079f580b464240cf64e9f
|
|
| BLAKE2b-256 |
047628e76d30815a554171d1179c6f90f0ed7c0d54c29da1ebd2e6b1dabf04da
|
File details
Details for the file python_media-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: python_media-0.1.0-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 26.1 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5de380c2b66758ae64614a69c8923e10b04de7ba01d5d0d68766cf6af9a9b2b2
|
|
| MD5 |
9b7a412d2ea726cc70187099d5bffc75
|
|
| BLAKE2b-256 |
9d7754f50931948417f70d7a5d4ee0cb2b7fab9bfbbe199a6cbc7018efcfa220
|
File details
Details for the file python_media-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: python_media-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 26.1 MB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
958ab7d02291c8f241ca9b114dda69c74283c5e2355fef92ad6032a028b7f53b
|
|
| MD5 |
8f8fdddc210f2d4c2cc263621df2d59e
|
|
| BLAKE2b-256 |
5e1ed6262a4675afbcf8a63a52177f6976baeff3fe3c63d88b55caaa964c561b
|
File details
Details for the file python_media-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: python_media-0.1.0-cp310-cp310-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 26.1 MB
- Tags: CPython 3.10, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0dfcdc2ec45089575f51a204bb9d4760edd36380911c211c2ed35001dd73492
|
|
| MD5 |
9111041ee48a4f3f91879d6ba2f6a558
|
|
| BLAKE2b-256 |
aa82d0c616d0c554260d7e4cd6ce50932246eaac179204538c12ff58eb9ae62e
|
File details
Details for the file python_media-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: python_media-0.1.0-cp39-cp39-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 26.1 MB
- Tags: CPython 3.9, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9f8856ac0e12c57e9b531c6b21d4760a7e4a1e67976f592dcf9b7115f5d827a
|
|
| MD5 |
39b8edb96ed982f1e9deea3d66800705
|
|
| BLAKE2b-256 |
6c04b757c88420b3e013444336d00489cfeba0aa94229b28e4e8033c3790d363
|