Metadata-Version: 2.5
Name: livephoto-to-gif
Version: 0.1.0
Summary: Python library to convert Live Photo videos into animated GIFs
Project-URL: Homepage, https://github.com/sherotree/livephoto-to-gif-py
Project-URL: Repository, https://github.com/sherotree/livephoto-to-gif-py
Project-URL: Web Tool, https://www.uwarp.design/livephoto-to-gif
Author-email: Sherotree <sherotree@users.noreply.github.com>
License-Expression: MIT
License-File: LICENSE
Keywords: animation,converter,ffmpeg,gif,live-photo,video
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Multimedia :: Video :: Conversion
Requires-Python: >=3.8
Provides-Extra: dev
Requires-Dist: black>=23.0.0; extra == 'dev'
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
Requires-Dist: pytest>=7.0.0; extra == 'dev'
Requires-Dist: ruff>=0.1.0; extra == 'dev'
Provides-Extra: test
Requires-Dist: pytest-cov>=4.0.0; extra == 'test'
Requires-Dist: pytest>=7.0.0; extra == 'test'
Description-Content-Type: text/markdown

# livephoto-to-gif

[**Live Photo to GIF**](https://www.uwarp.design/livephoto-to-gif) — free browser tool for converting Live Photo videos into animated GIFs (the reference UI for this library).

Offline Python library for converting Live Photo videos (MOV/MP4) and `.livp` containers into optimized animated GIFs using FFmpeg.

Package: **`livephoto-to-gif`** on PyPI (`0.1.0`).

## System Requirements

**FFmpeg must be installed** on your system and available in your `PATH`.

- **macOS**: `brew install ffmpeg`
- **Ubuntu/Debian**: `sudo apt install ffmpeg`
- **Windows**: Download from [ffmpeg.org](https://ffmpeg.org/download.html)

The library raises `FFmpegNotFoundError` if FFmpeg is missing. Conversion failures raise `ConversionError` with FFmpeg stderr details.

## Install

```bash
pip install livephoto-to-gif
```

## Quick start

```python
from livephoto_to_gif import convert_to_gif

# Convert a Live Photo video to GIF
convert_to_gif("live_photo.mov", "output.gif")

# Or let it auto-generate the output filename
convert_to_gif("live_photo.mov")  # creates live_photo.gif
```

### CLI

```bash
livephoto-to-gif live_photo.mov -o output.gif
livephoto-to-gif video.mp4 --fps 15 --width 480
livephoto-to-gif photo.livp -o photo.gif
```

## API

### Class API

| Method | Description |
| --- | --- |
| `LivePhotoToGifConverter(fps=15, width=480, loop=0)` | Initialize converter with output settings |
| `.convert(input_path, output_path=None)` | Convert video/`.livp` to GIF file (returns `Path`) |
| `.convert_to_bytes(input_path)` | Convert and return GIF bytes |

**Parameters:**
- `fps`: Output frame rate (default: 15)
- `width`: Max output width in pixels; height scales proportionally (default: 480)
- `loop`: Number of times to loop (0 = infinite, default: 0)
- `dither`: FFmpeg dithering algorithm (default: `"bayer"`)
- `scale_filter`: FFmpeg scaling filter (default: `"lanczos"`)

### Convenience functions

| Function | Role |
| --- | --- |
| `convert_to_gif(input_path, output_path=None, fps=15, width=480, loop=0)` | One-shot conversion |
| `is_live_photo_video(file_path)` | Check supported extension (`.mov`, `.mp4`, `.m4v`, `.livp`) |
| `extract_video_from_livp(livp_path, dest_dir)` | Extract embedded video from a `.livp` ZIP container |

### Exceptions

| Exception | When |
| --- | --- |
| `FFmpegNotFoundError` | `ffmpeg` binary not found in `PATH` |
| `ConversionError` | FFmpeg ran but conversion failed |

## Examples

### Basic conversion

```python
from livephoto_to_gif import convert_to_gif

convert_to_gif("live_photo.mov", "animation.gif")

output = convert_to_gif("video.mp4")
print(f"Created: {output}")  # Created: video.gif
```

### Custom output settings

```python
from livephoto_to_gif import LivePhotoToGifConverter

converter = LivePhotoToGifConverter(
    fps=20,
    width=640,
    loop=5,
)

converter.convert("video1.mov", "output1.gif")
converter.convert("video2.mp4", "output2.gif")
```

### `.livp` containers

Some Live Photo exports (especially Android) ship as a `.livp` ZIP archive containing a still image plus a short video. The library extracts the embedded `.mov`/`.mp4`/`.m4v` and converts it:

```python
from livephoto_to_gif import convert_to_gif

convert_to_gif("photo.livp", "photo.gif")
```

### Work with bytes in memory

```python
from livephoto_to_gif import LivePhotoToGifConverter

converter = LivePhotoToGifConverter()
gif_bytes = converter.convert_to_bytes("live_photo.mov")

with open("output.gif", "wb") as f:
    f.write(gif_bytes)
```

### Error handling

```python
from livephoto_to_gif import convert_to_gif, ConversionError, FFmpegNotFoundError

try:
    convert_to_gif("live_photo.mov", "output.gif")
except FFmpegNotFoundError:
    print("Please install FFmpeg: https://ffmpeg.org/download.html")
except FileNotFoundError:
    print("Input file not found")
except ValueError as e:
    print(f"Invalid input: {e}")
except ConversionError as e:
    print(f"Conversion failed: {e}")
```

## How it works

This library uses FFmpeg with a two-pass palette optimization:

1. **Palette generation**: FFmpeg analyzes the video and generates an optimized 256-color palette using `palettegen` with diff stats mode
2. **GIF encoding**: The video is re-encoded using `paletteuse` with the generated palette and Bayer dithering

**FFmpeg command equivalent:**

```bash
ffmpeg -i input.mov \
  -vf "fps=15,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen=stats_mode=diff[p];[s1][p]paletteuse=dither=bayer" \
  -loop 0 \
  output.gif
```

## Supported formats

| Format | Extension | Notes |
| --- | --- | --- |
| QuickTime | `.mov` | Typical Live Photo video export from iPhone |
| MP4 | `.mp4`, `.m4v` | Alternative video containers |
| Live Photo package | `.livp` | ZIP archive with an embedded video clip |

Export the **video** component of an Apple Live Photo (Share → Save to Files), not the still HEIC. Apple still+video pairs are not a single `.livp` file; use the exported `.mov`/`.mp4`.

## Tests

```bash
pip install -e ".[test]"
pytest
pytest --cov=livephoto_to_gif
```

Tests use committed fixtures under `tests/fixtures/` (`sample.mov`, `sample.mp4`, `sample.livp`). End-to-end conversion tests require FFmpeg.

## Development

```bash
pip install -e ".[dev]"
pytest
black src/ tests/
ruff check src/ tests/
```

## Technical details

- **Input**: MOV, MP4, M4V, or `.livp` (any video codec FFmpeg can decode)
- **Output**: Animated GIF (GIF89a) with optimized 256-color palette
- **Defaults**: 15 fps, 480px max width, infinite loop
- **Scaling**: Lanczos; **dithering**: Bayer
- **Optimization**: Two-pass `palettegen` / `paletteuse`

## License

MIT. See [LICENSE](./LICENSE).
