https://github.com/SocAIty/media-toolkit

Web-ready standardized file processing and serialization. Read, write, convert and send files. Including image, audio, video and any other file. Easily convert between numpy, base64, bytes and more.

https://github.com/SocAIty/media-toolkit

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.1%) to scientific vocabulary

Keywords

base64 bytesio fast-task-api httpx image-processing json numpy serialization video
Last synced: 5 months ago · JSON representation

Repository

Web-ready standardized file processing and serialization. Read, write, convert and send files. Including image, audio, video and any other file. Easily convert between numpy, base64, bytes and more.

Basic Info
  • Host: GitHub
  • Owner: SocAIty
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 4.33 MB
Statistics
  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 1
Topics
base64 bytesio fast-task-api httpx image-processing json numpy serialization video
Created over 1 year ago · Last pushed 6 months ago
Metadata Files
Readme Funding License

README.md

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 - Native FastSDK and FastTaskAPI integration - Supports httpx, requests

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.

```bash

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.

```python 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

newfile = ImageFile().fromany(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 fromany, because it definitely uses the correct method to load the file. ```python newfile = MediaFile()

newfile.fromfile("path/to/file") newfile.fromfile(open("path/to/file", "rb")) newfile.fromnumpyarray(myarray) newfile.frombytes(b'bytes') newfile.frombase64('base64string') newfile.fromstarletteuploadfile(starletteuploadfile)

```

Convert to any format or write to file

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

```python myfile = ImageFile().fromfile("path/to/my_image.png")

myfile.save("path/to/newfile.png")
asnumpyarray = myfile.tonumpyarray() asnumpyarray = np.array(myfile)

asbytes = myfile.tobytes() asbytes = bytes(myfile) asbase64 = myfile.tobase64() asjson = myfile.to_json() ```

Working with Collections of Files

MediaList

A flexible list that can handle multiple media files with type safety:

```python from media_toolkit import MediaList, AudioFile

Create a list that only accepts AudioFiles

audio_list = MediaListAudioFile

Add files to the list

audiolist.append("path/to/audio.mp3") audiolist.extend(["url1", "url2"])

Process all files

for audio in audiolist: print(audio.filesize())

Convert all files to base64

base64files = audiolist.to_base64() ```

MediaDict

A dictionary for organizing media files with keys:

```python from media_toolkit import MediaDict, ImageFile

Create a dictionary that only accepts ImageFiles

image_dict = MediaDictImageFile

Add files with keys

imagedict["profile"] = "path/to/profile.jpg" imagedict["banner"] = "https://example.com/banner.png"

Process files

for key, image in imagedict.items(): print(f"{key}: {image.filesize()}")

Convert to JSON

jsondata = imagedict.to_json() ```

Both MediaList and MediaDict support: - Type safety with generic types (e.g., MediaList[AudioFile]) - Lazy loading of files - Batch processing - Common operations (tobase64, tobytes, etc.) - Nested structures (MediaDict inside MediaList and vice versa)

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.

```python

load the video file

vf = VideoFile().fromfile("testfiles/testvid1.mp4")

extract audio_file

vf.extractaudio("extractedaudio.mp3")

stream the video

for img, audio in vf.tovideostream(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().fromdir("path/to/imagefolder", audio=f"extractedaudio.mp3", framerate=30)

create a video from a video stream

fromstream = VideoFile().fromvideostream(vf.tovideostream(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 taskendpoint function definition and transmitted data will be converted. Check out the FastTaskAPI documentation for more information. ```python from fasttask_api import ImageFile, AudioFile, VideoFile

@app.taskendpoint("/myfileupload") def myuploadimage(image: ImageFile, audio: AudioFile, video: VideoFile): imageasnparray = np.array(image) ```

fastAPI - services

You can use the files in fastapi and transform the starlette upload file to a MediaFile. python @app.post("/upload") async def upload_file(file: UploadFile = File(...)): mf = ImageFile().from_any(file) return {"filename": file.filename}

Client with: requests, httpx

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

```python import httpx

mymediafile = ImageFile().fromfile("path/to/myimage.png") myfiles = { "paramname": mymediafile.tohttpxsendabletuple() ... } 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) or on disk with temporary files.

ToDo:

  • [x] decreasing redundancies for fileinfo() method

Owner

  • Name: SocAIty
  • Login: SocAIty
  • Kind: organization
  • Location: Spain

Catalyzing the intelligence Revolution for a better socaity. We are democratizing AI access.

GitHub Events

Total
  • Watch event: 1
  • Push event: 23
Last Year
  • Watch event: 1
  • Push event: 23

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 35
  • Total Committers: 2
  • Avg Commits per committer: 17.5
  • Development Distribution Score (DDS): 0.029
Past Year
  • Commits: 35
  • Committers: 2
  • Avg Commits per committer: 17.5
  • Development Distribution Score (DDS): 0.029
Top Committers
Name Email Commits
w4hns1nn m****s@d****m 34
w4hns1nn 7****n 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 765 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 29
  • Total maintainers: 1
pypi.org: media-toolkit

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

  • Homepage: https://www.socaity.ai
  • Documentation: https://media-toolkit.readthedocs.io/
  • License: MIT License Copyright (c) Suno, Inc Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Latest release: 0.2.9
    published 6 months ago
  • Versions: 29
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 765 Last month
Rankings
Dependent packages count: 10.7%
Average: 35.6%
Dependent repos count: 60.4%
Maintainers (1)
Last synced: 6 months ago