pympanim


Namepympanim JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/tjstretchalot/pympanim
SummaryMultiprocessing-friendly animations
upload_time2024-10-02 22:05:01
maintainerNone
docs_urlNone
authorTimothy Moore
requires_python>=3.9
licenseCC0
keywords pympanim animations video mp4
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Multiprocessed Animations

## Summary

This library helps building movies in python from images. Specifically, it
intends to allow multiple threads to produce images which are stitched together
into movies with ffmpeg.

## Use-cases

This can be used to multithread the image creation process when using
matplotlib animations. FFMpeg will use multithreading to encode the images into
a video, however producing the images themselves is a bottleneck if you have
many cores available. Hence, this will vastly reduce the time it takes to
generate matplotlib animations.

This can also be used for generating scenes with
[PIL](https://pillow.readthedocs.io/en/stable/).

This library's goal is to make generating videos as simple as possible first,
then to go as fast as possible within those simple techniques. This gives fast
enough performance for many projects, and has the enormous benefit that you
can throw more hardware at the problem. Using ffmpeg directly without
multithreading image generation will not scale to more hardware.

## Performance

With ideal settings, the images should be generated at a rate that just barely
does not fill the ffmpeg process input pipe. This will ensure that images are
being generated as quickly as they can be encoded.

By default, this library will attempt to find the settings that accomplish this
task. This takes a bit of time to accomplish, so the final settings are exposed
and it can be helpful to use those when re-running roughly the same task. The
correct settings will depend on how long it takes to generate images and how
long it takes to encode them which varies based on the image statistics.

## Installation

`pip install pympanim`

## Dependencies

This depends on ffmpeg being installed. It can be installed
[here](https://ffmpeg.org/download.html). Other python dependencies will be
automatically installed by pip.

## Usage - Acts

### Motivation

Many times you have something which is capable of rendering some underlying
state in a consistent way, and the video you want to produce is made up of
parts that manipulate the state that is sent to the renderer. This use case
is handled specifically by `pympanim/acts.py`.

### Summary

Define the state that completely describes how to render things in a class that
subclasses `pympanim.acts.ActState`. Then create the thing which can render
the given state to an image (described via rgba bytes or a Pillow Image).

With these ready, create (one or more) `Scene`s, which are things that
manipulate the state you just created. A scene has some duration, and must be
able to set the state to correspond to a particular time within the scene.

This library will provide common manipulations of scenes - nonlinear time
manipulations, cropping, reversing, and sequencing. To make the most use of
these manipulations, Scenes should be as simple as possible.

Multiple Acts can be combined in much the same way. See Usage - Frame
Generators for details below.

To produce the video, use `pympanim.worker.produce`, creating a frame generator
out of the scenes using `pympanim.acts.Act`

### Boilerplate

```py
import PIL.Image
import pympanim.worker as pmaw
import pympanim.acts as acts
import pympanim.frame_gen as fg
import os

class MyActState(acts.ActState):
    pass

class MyActRenderer(acts.ActRenderer):
    @property
    def frame_size(self):
        return (1920, 1080) # in pixels

    def render(self, act_state: MyActState) -> bytes:
        with self.render_pil(act_state) as img:
            return fg.img_to_bytes(img)

    def render_pil(self, act_state: MyActState) -> PIL.Image.Image:
        # By default, render_pil delegates to render. It is shown reversed
        # here for completeness.
        return PIL.Image.new('RGBA', self.frame_size, 'white')

class Scene1(acts.Scene):
    @property
    def duration(self):
        return 1 # 1 millisecond; allows you to treat time_ms as % progress

    def apply(self, act_state: MyActState, time_ms: float, dbg: bool = False):
        # dbg is passed across the scene heirarchy and will be false when
        # rendering the video. you may find it helpful to print some debug
        # information when it is true
        pass

def _scene():
    scene = Scene1()
    return (acts.FluentScene(scene)
            .dilate(lambda x: x**2) # any easing works here. see
                                    # pympanim.easing and pytweening
            .time_rescale(1 / 10000) # 10s of real time corresponds to
                                     # 1ms of scene time
            .build()
           )

def _main():
    os.makedirs('out', exist_ok=True)
    act_state = MyActState()
    act_renderer = MyActRenderer()

    pmaw.produce(
        acts.Act(act_state, renderer, [_scene()]),
        fps=60,
        dpi=100,
        bitrate=-1,
        outfile='out/video.mp4'
    )

if __name__ == '__main__':
    _main()

```

For method-level documentation, use the built-in `help` command, i.e.,
```
>python3
>>> import pympanim.acts
>>> help(pympanim.acts)
Help on module pympanim.acts in pympanim:
.. (omitted for readme brevity) ..
```

## Usage - Frame Generators

### Motivation

The most novel part of this library is the boilerplate to generate videos where
the image generation itself is multithreaded. This is exposed in as raw a manner
as possible using `pympanim/frame_gen.py` which is merely wrapped by
`pympanim/acts.py`. This section discusses how to use this library with minimal
abstraction.

### Summary

Create a subclass of `pympanim.frame_gen.FrameGenerator`, which requires
defining a duration, frame size in pixels, and a `generate_at` function which
can generate an image given just the time within the video.

This library provides common manipulations of vidoes that you can wrap your
video with, such as cropping, time dilations, reversing time, and combinations
of frame generators.

To produce the video, use `pympanim.worker.produce`. Everything else is handled
for you, including reasonable guesses for performance settings and runtime
performance tuning.

### Boilerplate

```py
import PIL.Image
import pympanim.frame_gen as fg
import pympanim.worker as pmaw
import os

class MyFrameGenerator(fg.FrameGenerator):
    @property
    def duration(self):
        return 1 # 1 ms, allows you to treat time_ms as % progress

    @property
    def frame_size(self):
        return (1920, 1080) # in pixels

    def generate_at(self, time_ms):
        # by default generate_at_pil delegates to generate_at. we show the
        # reverse for completeness
        with self.generate_at_pil(time_ms) as img:
            return fg.img_to_bytes(img)

    def generate_at_pil(self, time_ms):
        # this from white to red, you can do whatever
        return PIL.Image.new('RGBA', self.frame_size, f'#{int(time_ms*255):02x}0000')

def _fg():
    base = MyFrameGenerator()

    # random example of the stuff you can do
    return (fg.FluentFG(base)
            .time_rescale(1 / 10000)         # 10s long
            .then(                           # after current
                fg.FluentFG(base)            # play again
                    .time_rescale(1 / 10000) # also 10s long
                    .reverse()               # but this time in reverse
                    .build()
            )
            .build())

def _main():
    os.makedirs('out', exist_ok=True)

    pmaw.produce(
        _fg(),
        fps=60,
        dps=100,
        bitrate=-1,
        outfile='out/video.mp4'
    )

if __name__ == '__main__':
    _main()

```


## Examples

The examples/ folder has the sourcecode for the following examples:

```
python3 -m examples.redsquare
```

Produces https://gfycat.com/digitalmaleflyingfish

```
python3 -m examples.mpl_line
```

Produces https://gfycat.com/wickedpracticalattwatersprairiechicken

## Known Issues

If processes are forked instead of spawned, text will appear spastic in
matplotlib. In general, matplotlib does not handle forked processes well.
The worker attempts to force spawning mode instead of fork-mode, but this
will fail if the process is already forked. If you are experiencing weird
or glitchy videos, include this *before any other imports* which might be
spawning processes (e.g. torch or torchvision).

```py
import multiprocessing as mp

try:
    mp.set_start_method('spawn')
except RuntimeError:
    pass
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tjstretchalot/pympanim",
    "name": "pympanim",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "pympanim animations video mp4",
    "author": "Timothy Moore",
    "author_email": "mtimothy984@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/02/19/d88d9d08c6e4f461c41e4ecc2d2cd1512981f270b7e2b677671a840d620b/pympanim-0.1.1.tar.gz",
    "platform": null,
    "description": "# Multiprocessed Animations\r\n\r\n## Summary\r\n\r\nThis library helps building movies in python from images. Specifically, it\r\nintends to allow multiple threads to produce images which are stitched together\r\ninto movies with ffmpeg.\r\n\r\n## Use-cases\r\n\r\nThis can be used to multithread the image creation process when using\r\nmatplotlib animations. FFMpeg will use multithreading to encode the images into\r\na video, however producing the images themselves is a bottleneck if you have\r\nmany cores available. Hence, this will vastly reduce the time it takes to\r\ngenerate matplotlib animations.\r\n\r\nThis can also be used for generating scenes with\r\n[PIL](https://pillow.readthedocs.io/en/stable/).\r\n\r\nThis library's goal is to make generating videos as simple as possible first,\r\nthen to go as fast as possible within those simple techniques. This gives fast\r\nenough performance for many projects, and has the enormous benefit that you\r\ncan throw more hardware at the problem. Using ffmpeg directly without\r\nmultithreading image generation will not scale to more hardware.\r\n\r\n## Performance\r\n\r\nWith ideal settings, the images should be generated at a rate that just barely\r\ndoes not fill the ffmpeg process input pipe. This will ensure that images are\r\nbeing generated as quickly as they can be encoded.\r\n\r\nBy default, this library will attempt to find the settings that accomplish this\r\ntask. This takes a bit of time to accomplish, so the final settings are exposed\r\nand it can be helpful to use those when re-running roughly the same task. The\r\ncorrect settings will depend on how long it takes to generate images and how\r\nlong it takes to encode them which varies based on the image statistics.\r\n\r\n## Installation\r\n\r\n`pip install pympanim`\r\n\r\n## Dependencies\r\n\r\nThis depends on ffmpeg being installed. It can be installed\r\n[here](https://ffmpeg.org/download.html). Other python dependencies will be\r\nautomatically installed by pip.\r\n\r\n## Usage - Acts\r\n\r\n### Motivation\r\n\r\nMany times you have something which is capable of rendering some underlying\r\nstate in a consistent way, and the video you want to produce is made up of\r\nparts that manipulate the state that is sent to the renderer. This use case\r\nis handled specifically by `pympanim/acts.py`.\r\n\r\n### Summary\r\n\r\nDefine the state that completely describes how to render things in a class that\r\nsubclasses `pympanim.acts.ActState`. Then create the thing which can render\r\nthe given state to an image (described via rgba bytes or a Pillow Image).\r\n\r\nWith these ready, create (one or more) `Scene`s, which are things that\r\nmanipulate the state you just created. A scene has some duration, and must be\r\nable to set the state to correspond to a particular time within the scene.\r\n\r\nThis library will provide common manipulations of scenes - nonlinear time\r\nmanipulations, cropping, reversing, and sequencing. To make the most use of\r\nthese manipulations, Scenes should be as simple as possible.\r\n\r\nMultiple Acts can be combined in much the same way. See Usage - Frame\r\nGenerators for details below.\r\n\r\nTo produce the video, use `pympanim.worker.produce`, creating a frame generator\r\nout of the scenes using `pympanim.acts.Act`\r\n\r\n### Boilerplate\r\n\r\n```py\r\nimport PIL.Image\r\nimport pympanim.worker as pmaw\r\nimport pympanim.acts as acts\r\nimport pympanim.frame_gen as fg\r\nimport os\r\n\r\nclass MyActState(acts.ActState):\r\n    pass\r\n\r\nclass MyActRenderer(acts.ActRenderer):\r\n    @property\r\n    def frame_size(self):\r\n        return (1920, 1080) # in pixels\r\n\r\n    def render(self, act_state: MyActState) -> bytes:\r\n        with self.render_pil(act_state) as img:\r\n            return fg.img_to_bytes(img)\r\n\r\n    def render_pil(self, act_state: MyActState) -> PIL.Image.Image:\r\n        # By default, render_pil delegates to render. It is shown reversed\r\n        # here for completeness.\r\n        return PIL.Image.new('RGBA', self.frame_size, 'white')\r\n\r\nclass Scene1(acts.Scene):\r\n    @property\r\n    def duration(self):\r\n        return 1 # 1 millisecond; allows you to treat time_ms as % progress\r\n\r\n    def apply(self, act_state: MyActState, time_ms: float, dbg: bool = False):\r\n        # dbg is passed across the scene heirarchy and will be false when\r\n        # rendering the video. you may find it helpful to print some debug\r\n        # information when it is true\r\n        pass\r\n\r\ndef _scene():\r\n    scene = Scene1()\r\n    return (acts.FluentScene(scene)\r\n            .dilate(lambda x: x**2) # any easing works here. see\r\n                                    # pympanim.easing and pytweening\r\n            .time_rescale(1 / 10000) # 10s of real time corresponds to\r\n                                     # 1ms of scene time\r\n            .build()\r\n           )\r\n\r\ndef _main():\r\n    os.makedirs('out', exist_ok=True)\r\n    act_state = MyActState()\r\n    act_renderer = MyActRenderer()\r\n\r\n    pmaw.produce(\r\n        acts.Act(act_state, renderer, [_scene()]),\r\n        fps=60,\r\n        dpi=100,\r\n        bitrate=-1,\r\n        outfile='out/video.mp4'\r\n    )\r\n\r\nif __name__ == '__main__':\r\n    _main()\r\n\r\n```\r\n\r\nFor method-level documentation, use the built-in `help` command, i.e.,\r\n```\r\n>python3\r\n>>> import pympanim.acts\r\n>>> help(pympanim.acts)\r\nHelp on module pympanim.acts in pympanim:\r\n.. (omitted for readme brevity) ..\r\n```\r\n\r\n## Usage - Frame Generators\r\n\r\n### Motivation\r\n\r\nThe most novel part of this library is the boilerplate to generate videos where\r\nthe image generation itself is multithreaded. This is exposed in as raw a manner\r\nas possible using `pympanim/frame_gen.py` which is merely wrapped by\r\n`pympanim/acts.py`. This section discusses how to use this library with minimal\r\nabstraction.\r\n\r\n### Summary\r\n\r\nCreate a subclass of `pympanim.frame_gen.FrameGenerator`, which requires\r\ndefining a duration, frame size in pixels, and a `generate_at` function which\r\ncan generate an image given just the time within the video.\r\n\r\nThis library provides common manipulations of vidoes that you can wrap your\r\nvideo with, such as cropping, time dilations, reversing time, and combinations\r\nof frame generators.\r\n\r\nTo produce the video, use `pympanim.worker.produce`. Everything else is handled\r\nfor you, including reasonable guesses for performance settings and runtime\r\nperformance tuning.\r\n\r\n### Boilerplate\r\n\r\n```py\r\nimport PIL.Image\r\nimport pympanim.frame_gen as fg\r\nimport pympanim.worker as pmaw\r\nimport os\r\n\r\nclass MyFrameGenerator(fg.FrameGenerator):\r\n    @property\r\n    def duration(self):\r\n        return 1 # 1 ms, allows you to treat time_ms as % progress\r\n\r\n    @property\r\n    def frame_size(self):\r\n        return (1920, 1080) # in pixels\r\n\r\n    def generate_at(self, time_ms):\r\n        # by default generate_at_pil delegates to generate_at. we show the\r\n        # reverse for completeness\r\n        with self.generate_at_pil(time_ms) as img:\r\n            return fg.img_to_bytes(img)\r\n\r\n    def generate_at_pil(self, time_ms):\r\n        # this from white to red, you can do whatever\r\n        return PIL.Image.new('RGBA', self.frame_size, f'#{int(time_ms*255):02x}0000')\r\n\r\ndef _fg():\r\n    base = MyFrameGenerator()\r\n\r\n    # random example of the stuff you can do\r\n    return (fg.FluentFG(base)\r\n            .time_rescale(1 / 10000)         # 10s long\r\n            .then(                           # after current\r\n                fg.FluentFG(base)            # play again\r\n                    .time_rescale(1 / 10000) # also 10s long\r\n                    .reverse()               # but this time in reverse\r\n                    .build()\r\n            )\r\n            .build())\r\n\r\ndef _main():\r\n    os.makedirs('out', exist_ok=True)\r\n\r\n    pmaw.produce(\r\n        _fg(),\r\n        fps=60,\r\n        dps=100,\r\n        bitrate=-1,\r\n        outfile='out/video.mp4'\r\n    )\r\n\r\nif __name__ == '__main__':\r\n    _main()\r\n\r\n```\r\n\r\n\r\n## Examples\r\n\r\nThe examples/ folder has the sourcecode for the following examples:\r\n\r\n```\r\npython3 -m examples.redsquare\r\n```\r\n\r\nProduces https://gfycat.com/digitalmaleflyingfish\r\n\r\n```\r\npython3 -m examples.mpl_line\r\n```\r\n\r\nProduces https://gfycat.com/wickedpracticalattwatersprairiechicken\r\n\r\n## Known Issues\r\n\r\nIf processes are forked instead of spawned, text will appear spastic in\r\nmatplotlib. In general, matplotlib does not handle forked processes well.\r\nThe worker attempts to force spawning mode instead of fork-mode, but this\r\nwill fail if the process is already forked. If you are experiencing weird\r\nor glitchy videos, include this *before any other imports* which might be\r\nspawning processes (e.g. torch or torchvision).\r\n\r\n```py\r\nimport multiprocessing as mp\r\n\r\ntry:\r\n    mp.set_start_method('spawn')\r\nexcept RuntimeError:\r\n    pass\r\n```\r\n",
    "bugtrack_url": null,
    "license": "CC0",
    "summary": "Multiprocessing-friendly animations",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/tjstretchalot/pympanim"
    },
    "split_keywords": [
        "pympanim",
        "animations",
        "video",
        "mp4"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f8c20e00dfd8c637fb7a1c78bf24ae38201787ff8fda0e6edd3aedd6764d41f6",
                "md5": "0fd7297bb8d806a9404f3b1e633b9188",
                "sha256": "ab742c53b096f3eb7a8f727c67aefd5dc1e04fbef02783fe031a8be690a5d879"
            },
            "downloads": -1,
            "filename": "pympanim-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0fd7297bb8d806a9404f3b1e633b9188",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 36529,
            "upload_time": "2024-10-02T22:04:59",
            "upload_time_iso_8601": "2024-10-02T22:04:59.643979Z",
            "url": "https://files.pythonhosted.org/packages/f8/c2/0e00dfd8c637fb7a1c78bf24ae38201787ff8fda0e6edd3aedd6764d41f6/pympanim-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0219d88d9d08c6e4f461c41e4ecc2d2cd1512981f270b7e2b677671a840d620b",
                "md5": "edf42cbbd56178dd6a2e6e99b914ea4a",
                "sha256": "5143a10cb6de19104b0ef6755f70f7a029785405b9bd6cd453afda9ecb44b209"
            },
            "downloads": -1,
            "filename": "pympanim-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "edf42cbbd56178dd6a2e6e99b914ea4a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 36027,
            "upload_time": "2024-10-02T22:05:01",
            "upload_time_iso_8601": "2024-10-02T22:05:01.195272Z",
            "url": "https://files.pythonhosted.org/packages/02/19/d88d9d08c6e4f461c41e4ecc2d2cd1512981f270b7e2b677671a840d620b/pympanim-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-02 22:05:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tjstretchalot",
    "github_project": "pympanim",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "pympanim"
}
        
Elapsed time: 2.27340s