Skip to main content

Web-ready standardized file processing and serialization. Read, load and convert to standard file types with a common interface.

Project description

MediaToolkit

Web-ready standardized file processing and serialization

Features

Read, load and convert to standard file types with a common interface. Especially useful for code that works with multiple file types like images, audio, video, etc.

Load and convert from and to common data types:

  • numpy arrays
  • file paths
  • bytes,
  • base64
  • json
  • urls
  • etc.

Transmit files between services with a common interface

Work with native python libs like BytesIO.

Only use the file types you need, no unnecessary dependencies.

Installation

You can install the package with PIP, or clone the repository.

# install from pypi
pip install media-toolkit
# install without dependencies: this is useful if you only need the basic functionality (working with files)
pip install media-toolkit --no-deps
# if you want to use certain file types, and convenience functions
pip install media-toolkit[VideoFile]  # or [AudioFile, VideoFile, ...]
# install from github for newest release
pip install git+git://github.com/SocAIty/media-toolkit

The package checks if you have missing dependencies for certain file types while using. Use the --no-deps flag for a minimal tiny pure python installation. The package with dependencies is quite small < 39kb itself.

Note: for VideoFile you will also need to install ffmpeg

Usage

Create a media-file from any data type

The library automatically detects the data type and loads it correctly.

from media_toolkit import MediaFile, ImageFile, AudioFile, VideoFile

# could be a path, url, base64, bytesio, file_handle, numpy array ...
arbitrary_data = "...."
# Instantiate an image file
new_file = ImageFile().from_any(arbitrary_data)

All files (ImageFile, AudioFile, VideoFile) types support the same interface / methods.

Explicitly load from a certain type.

This method is more secure than from_any, because it definitely uses the correct method to load the file.

new_file = MediaFile()

new_file.from_file("path/to/file")
new_file.from_file(open("path/to/file", "rb"))
new_file.from_numpy_array(my_array)
new_file.from_bytes(b'bytes')
new_file.from_base64('base64string')
new_file.from_starlette_upload_file(starlette_upload_file)

Convert to any format or write to file

Supports common serialization methods like bytes(), np.array(), dict()

my_file = ImageFile().from_file("path/to/my_image.png")

my_file.save("path/to/new_file.png")  
as_numpy_array = my_file.to_numpy_array()
as_numpy_array = np.array(my_file)

as_bytes = my_file.to_bytes()
as_bytes = bytes(my_file)
as_base64 = my_file.to_base64()
as_json = my_file.to_json()

Working with VideoFiles.

The VideoFiles wrap the famous vidgear package as well as pydub. VideoFiles support extra methods like audio extraction, combining video and audio. Vidgear is a powerful video processing library that supports many video formats and codecs and is known for fast video processing.

# load the video file
vf = VideoFile().from_file("test_files/test_vid_1.mp4")

# extract audio_file
vf.extract_audio("extracted_audio.mp3")

# stream the video
for img, audio in vf.to_video_stream(include_audio=True):
    cv2.imwrite("outtest.png", img)

# add audio to an videofile (supports files and numpy.array)
vf.add_audio("path/to/audio.mp3")

# create a video from a folder
VideoFile().from_dir("path/to/image_folder", audio=f"extracted_audio.mp3", frame_rate=30)

# create a video from a video stream
fromstream = VideoFile().from_video_stream(vf.to_video_stream(include_audio=True))

Web-features

We intent to make transmitting files between services as easy as possible. Here are some examples for services and clients.

FastTaskAPI - Services

The library supports the FastTaskAPI and FastSDK for easy file transmission between services. Simply use the files in the task_endpoint function definition and transmitted data will be converted. Check out the FastTaskAPI documentation for more information.

from fast_task_api import ImageFile, AudioFile, VideoFile

@app.task_endpoint("/my_file_upload")
def my_upload_image(image: ImageFile, audio: AudioFile, video: VideoFile):
    image_as_np_array = np.array(image)

fastAPI - services

You can use the files in fastapi and transform the starlette upload file to a MediaFile.

@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
    mf = ImageFile().from_starlette_upload_file(file)
    return {"filename": file.filename}

Client with: requests, httpx

To send a MediaFile to an openapi endpoint you can use the following method:

import httpx

my_media_file = ImageFile().from_file("path/to/my_image.png")
my_files = {
  "param_name": my_media_file.to_httpx_send_able_tuple()
  ...
}
response = httpx.Client().post(url, files=my_files)

How it works

If media-file is instantiated with from_* it converts it to an intermediate representation. The to_* methods then convert it to the desired format.

Currently the intermediate representation is supported in memory with (BytesIO).

ToDo:

  • additionally support tempfile backend instead of working bytesio memory mode only.
  • decreasing redundancies for _file_info() method

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

media_toolkit-0.1.1.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

media_toolkit-0.1.1-py3-none-any.whl (32.0 kB view details)

Uploaded Python 3

File details

Details for the file media_toolkit-0.1.1.tar.gz.

File metadata

  • Download URL: media_toolkit-0.1.1.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.11

File hashes

Hashes for media_toolkit-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9396da36a551b042644c595dbc0d4f46db560b783a0b2f27a61f7c411e04796e
MD5 f282261b02662e73ac64b6fd527f5350
BLAKE2b-256 915edeca67cc5658b3109ae889cae664b565becbbc3912e25f524127863a8c3c

See more details on using hashes here.

File details

Details for the file media_toolkit-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for media_toolkit-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a81d8c6365cb6f673cdc7479f1cb7e4e2358424c79181950446011f4e5b7cf74
MD5 1ecce63829ba6cd3dabfcf8a8d967d8a
BLAKE2b-256 5dc618aca7af72bfded860172f1c1c16e2fd338fc2cfaeba3317bd39d81ab0eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page