playa-ffmpeg 8.0.3

Safe FFmpeg wrapper with vcpkg integration for simplified cross-platform builds (FFmpeg 8.0, Rust 2024)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
# Developer Guide - rust-ffmpeg

Complete development setup for Windows, Linux, and macOS with FFmpeg 8.0 support.

---

## Table of Contents

- [Quick Start]#quick-start
- [Windows Setup]#windows-setup
- [Linux Setup]#linux-setup
- [macOS Setup]#macos-setup
- [Building Examples]#building-examples
- [Platform Differences]#platform-differences
- [Troubleshooting]#troubleshooting
- [vcpkg Details]#vcpkg-details

---

## Quick Start

### Prerequisites (All Platforms)

- **Rust** 1.75+ (edition 2024)
- **Clang/LLVM** (for bindgen)
- **pkg-config** (Linux/macOS)
- **FFmpeg 3.4 - 8.0** (system or vcpkg)

### Clone and Build

```bash
git clone https://github.com/zmwangx/rust-ffmpeg.git
cd rust-ffmpeg

# Build
cargo build --release

# Run example
cargo run --example video-info -- video.mp4
```

---

## Windows Setup

### Method 1: vcpkg (Recommended)

**1. Install vcpkg:**

```powershell
# Clone vcpkg
git clone https://github.com/microsoft/vcpkg.git C:\vcpkg
cd C:\vcpkg
.\bootstrap-vcpkg.bat

# Set environment variable
$env:VCPKG_ROOT = "C:\vcpkg"
[Environment]::SetEnvironmentVariable("VCPKG_ROOT", "C:\vcpkg", "User")
```

**2. Install FFmpeg:**

```powershell
# Static linking (recommended)
vcpkg install ffmpeg:x64-windows-static-md

# Or dynamic linking
vcpkg install ffmpeg:x64-windows

# Or fully static (including CRT)
vcpkg install ffmpeg:x64-windows-static
```

**3. Install LLVM (for bindgen):**

```powershell
# Via Chocolatey
choco install llvm

# Or download from https://releases.llvm.org/
# Add to PATH: C:\Program Files\LLVM\bin
```

**4. Build:**

```powershell
cargo build --release
```

**Notes:**
- `.cargo/config.toml` already includes Windows system libraries
- Static build creates single `.exe` (~20-60 MB)
- No DLLs needed for deployment

### Method 2: Manual FFmpeg

**1. Download FFmpeg:**

From https://www.gyan.dev/ffmpeg/builds/
- Download "Shared" (for dev) or "Static" (for distribution)

**2. Set environment:**

```powershell
$env:FFMPEG_DIR = "C:\ffmpeg"
$env:PATH = "C:\ffmpeg\bin;$env:PATH"
```

**3. Build:**

```powershell
cargo build --release
```

---

## Linux Setup

### Method 1: System FFmpeg (Simplest)

**Ubuntu/Debian:**

```bash
# Install FFmpeg development libraries
sudo apt update
sudo apt install -y \
    libavcodec-dev \
    libavformat-dev \
    libavutil-dev \
    libavdevice-dev \
    libavfilter-dev \
    libswscale-dev \
    libswresample-dev \
    pkg-config \
    clang

# Build
cargo build --release
```

**Fedora/RHEL:**

```bash
# Enable RPM Fusion for FFmpeg
sudo dnf install -y \
    https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm

# Install FFmpeg
sudo dnf install -y \
    ffmpeg-devel \
    clang \
    pkg-config

# Build
cargo build --release
```

**Arch Linux:**

```bash
sudo pacman -S ffmpeg clang pkg-config
cargo build --release
```

### Method 2: vcpkg (Static Linking)

**1. Install vcpkg:**

```bash
# Clone vcpkg
git clone https://github.com/microsoft/vcpkg.git ~/vcpkg
cd ~/vcpkg
./bootstrap-vcpkg.sh

# Add to PATH
echo 'export VCPKG_ROOT="$HOME/vcpkg"' >> ~/.bashrc
echo 'export PATH="$VCPKG_ROOT:$PATH"' >> ~/.bashrc
source ~/.bashrc
```

**2. Install dependencies:**

```bash
# Ubuntu/Debian
sudo apt install -y curl zip unzip tar pkg-config clang

# Fedora
sudo dnf install -y curl zip unzip tar pkg-config clang

# Arch
sudo pacman -S curl zip unzip tar pkg-config clang
```

**3. Install FFmpeg:**

```bash
# Static linking
vcpkg install ffmpeg:x64-linux

# Build
cargo build --release
```

**4. Create `.cargo/config.toml` (if not exists):**

```toml
# .cargo/config.toml for Linux
[target.x86_64-unknown-linux-gnu]
rustflags = [
    # Optional: static linking for deployment
    # "-C", "target-feature=+crt-static",
]
```

### Method 3: Build FFmpeg from Source

```bash
# Install build dependencies
sudo apt install -y \
    build-essential \
    yasm \
    nasm \
    libx264-dev \
    libx265-dev \
    libvpx-dev \
    libopus-dev

# Download and build FFmpeg
wget https://ffmpeg.org/releases/ffmpeg-8.0.tar.xz
tar xf ffmpeg-8.0.tar.xz
cd ffmpeg-8.0

./configure \
    --prefix=/usr/local \
    --enable-shared \
    --enable-gpl \
    --enable-libx264 \
    --enable-libx265 \
    --enable-libvpx \
    --enable-libopus

make -j$(nproc)
sudo make install
sudo ldconfig

# Build rust-ffmpeg
cd ~/rust-ffmpeg
cargo build --release
```

---

## macOS Setup

### Method 1: Homebrew (Recommended)

**1. Install Homebrew:**

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

**2. Install FFmpeg:**

```bash
# Install FFmpeg with libraries
brew install ffmpeg pkg-config

# Install LLVM (for bindgen)
brew install llvm

# Add LLVM to PATH
echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
```

**3. Build:**

```bash
cargo build --release
```

### Method 2: vcpkg (Static Linking)

**1. Install vcpkg:**

```bash
git clone https://github.com/microsoft/vcpkg.git ~/vcpkg
cd ~/vcpkg
./bootstrap-vcpkg.sh

echo 'export VCPKG_ROOT="$HOME/vcpkg"' >> ~/.zshrc
echo 'export PATH="$VCPKG_ROOT:$PATH"' >> ~/.zshrc
source ~/.zshrc
```

**2. Install FFmpeg:**

```bash
# Static linking
vcpkg install ffmpeg:arm64-osx  # Apple Silicon
# or
vcpkg install ffmpeg:x64-osx    # Intel Mac

# Build
cargo build --release
```

### Method 3: MacPorts

```bash
sudo port install ffmpeg pkgconfig
cargo build --release
```

---

## Building Examples

### video-info Example

Analyzes video files and tests FFmpeg functionality.

```bash
# Build
cargo build --example video-info --release

# Run
./target/release/examples/video_info video.mp4
```

**Expected output:**
```
=== FFmpeg Video Analyzer ===

File: video.mp4

📄 FILE METADATA
  Format: mov,mp4,m4a,3gp,3g2,mj2
  Duration: 10.00s
  Bitrate: 2.50 Mbps

📺 STREAMS (2 total)
  Stream #0 - Video: H264, 1920x1080, 30fps
  Stream #1 - Audio: AAC, 48000Hz, 2ch

✅ Analysis complete!
```

### All Examples

```bash
# Build all examples
cargo build --examples --release

# List built examples
ls -lh target/release/examples/

# Run specific example
cargo run --example transcode-x264 -- input.mp4 output.mp4
```

---

## Platform Differences

### Windows

**Linking:**
- Static: Requires Windows system libraries (bcrypt, user32, ole32, etc.)
- Dynamic: Requires DLLs in PATH or app directory
- Config: `.cargo/config.toml` includes all system libs

**Binary Size:**
- Static: ~20-60 MB (FFmpeg embedded)
- Dynamic: ~500 KB + DLLs (~20 MB)

**Deployment:**
- Static: Single `.exe`, works anywhere
- Dynamic: Distribute `.exe` + FFmpeg DLLs

### Linux

**Linking:**
- System FFmpeg: Dynamic by default
- vcpkg: Static linking available
- Usually smaller than Windows (~10-30 MB static)

**Library Paths:**
```bash
# Check FFmpeg libraries
pkg-config --libs libavcodec libavformat libavutil

# Check library location
ldconfig -p | grep libav
```

**Deployment:**
- Static: Single binary, portable
- Dynamic: May need to ship libs or use AppImage/Flatpak

### macOS

**Linking:**
- Homebrew: Dynamic linking
- vcpkg: Static linking
- Universal binaries supported (Intel + ARM)

**Library Paths:**
```bash
# Homebrew libs location
ls /opt/homebrew/lib/libav*  # Apple Silicon
ls /usr/local/lib/libav*     # Intel Mac
```

**Deployment:**
- Static: Single binary
- Dynamic: Use `install_name_tool` or bundle libs in .app

---

## Troubleshooting

### "FFmpeg not found"

**Linux/macOS:**
```bash
# Check pkg-config can find FFmpeg
pkg-config --modversion libavcodec

# If not found, set PKG_CONFIG_PATH
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH"
```

**Windows:**
```powershell
# Check vcpkg installation
vcpkg list | Select-String ffmpeg

# Set VCPKG_ROOT if not detected
$env:VCPKG_ROOT = "C:\vcpkg"
```

### "clang not found" or bindgen errors

**Ubuntu/Debian:**
```bash
sudo apt install clang libclang-dev
export LIBCLANG_PATH=/usr/lib/llvm-14/lib  # Adjust version
```

**macOS:**
```bash
brew install llvm
export PATH="/opt/homebrew/opt/llvm/bin:$PATH"
```

**Windows:**
```powershell
# Install LLVM
choco install llvm

# Or download: https://releases.llvm.org/
# Set LIBCLANG_PATH if needed
$env:LIBCLANG_PATH = "C:\Program Files\LLVM\bin"
```

### Linker errors on Windows

**Missing system libraries:**

Create `.cargo/config.toml`:
```toml
[target.x86_64-pc-windows-msvc]
rustflags = [
    "-l", "bcrypt",
    "-l", "user32",
    "-l", "ole32",
    "-l", "oleaut32",
    "-l", "mfuuid",
    "-l", "strmiids",
    "-l", "mfplat",
    "-l", "secur32",
    "-l", "ws2_32",
    "-l", "shlwapi",
    "-l", "gdi32",
    "-l", "vfw32",
]
```

### "undefined reference" errors on Linux

**Missing libraries:**
```bash
# Install missing dev packages
sudo apt install -y \
    libavcodec-dev \
    libavformat-dev \
    libavutil-dev \
    libswscale-dev \
    libswresample-dev

# Or use vcpkg for static linking
vcpkg install ffmpeg:x64-linux
```

### macOS dylib errors

**Missing libraries:**
```bash
# Install FFmpeg
brew install ffmpeg

# Check library paths
otool -L target/release/examples/video_info

# Fix library paths (if needed)
install_name_tool -change \
    /old/path/libavcodec.dylib \
    /opt/homebrew/lib/libavcodec.dylib \
    target/release/examples/video_info
```

### vcpkg build takes too long

FFmpeg compilation can take 30-60 minutes on first install.

**Speed up:**
```bash
# Use binary cache (if available)
vcpkg install ffmpeg --binarysource=clear

# Or use prebuilt system packages
# Linux: Use apt/dnf instead of vcpkg
# macOS: Use Homebrew instead of vcpkg
# Windows: Download prebuilt from gyan.dev
```

---

## vcpkg Details

### Triplets Explained

| Triplet | Platform | Linking | CRT | Use Case |
|---------|----------|---------|-----|----------|
| `x64-windows` | Windows x64 | Dynamic | Dynamic | Development |
| `x64-windows-static` | Windows x64 | Static | Static | Fully static |
| `x64-windows-static-md` | Windows x64 | Static | Dynamic | Rust default |
| `x64-linux` | Linux x64 | Static | - | Portable binary |
| `arm64-osx` | macOS ARM | Static | - | Apple Silicon |
| `x64-osx` | macOS x64 | Static | - | Intel Mac |

### Installing Specific FFmpeg Version

```bash
# List available versions
vcpkg search ffmpeg

# Install specific version (if available)
vcpkg install ffmpeg@8.0.0:x64-windows-static-md

# Or use vcpkg.json manifest
cat > vcpkg.json <<EOF
{
  "dependencies": [
    {
      "name": "ffmpeg",
      "version>=": "8.0.0"
    }
  ]
}
EOF
```

### Custom vcpkg Features

```bash
# Install with specific features
vcpkg install ffmpeg[core,avcodec,avformat,swscale]:x64-windows-static-md

# Available features
vcpkg search ffmpeg --x-full-desc
```

---

## Cross-Compilation

### Linux → Windows (via MinGW)

```bash
# Install cross-compiler
sudo apt install -y mingw-w64

# Add Rust target
rustup target add x86_64-pc-windows-gnu

# Configure .cargo/config.toml
cat >> .cargo/config.toml <<EOF
[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
EOF

# Build FFmpeg for Windows (via vcpkg)
vcpkg install ffmpeg:x64-mingw-static

# Cross-compile
cargo build --release --target x86_64-pc-windows-gnu
```

### macOS → iOS/tvOS

Requires additional FFmpeg build for iOS.

```bash
# Add target
rustup target add aarch64-apple-ios

# Build FFmpeg for iOS (manual or via xcframework)
# See: https://github.com/kewlbear/FFmpeg-iOS-build-script

# Cross-compile
cargo build --release --target aarch64-apple-ios
```

---

## Environment Variables

### Common Variables

| Variable | Purpose | Example |
|----------|---------|---------|
| `VCPKG_ROOT` | vcpkg installation | `/home/user/vcpkg` |
| `FFMPEG_DIR` | FFmpeg installation | `/usr/local` |
| `PKG_CONFIG_PATH` | pkg-config search path | `/usr/local/lib/pkgconfig` |
| `LIBCLANG_PATH` | Clang library path | `/usr/lib/llvm-14/lib` |

### Setting Environment Variables

**Linux/macOS (bash/zsh):**
```bash
# Temporary (current session)
export VCPKG_ROOT="$HOME/vcpkg"

# Permanent
echo 'export VCPKG_ROOT="$HOME/vcpkg"' >> ~/.bashrc
source ~/.bashrc
```

**Windows (PowerShell):**
```powershell
# Temporary (current session)
$env:VCPKG_ROOT = "C:\vcpkg"

# Permanent (user)
[Environment]::SetEnvironmentVariable("VCPKG_ROOT", "C:\vcpkg", "User")

# Permanent (system - requires admin)
[Environment]::SetEnvironmentVariable("VCPKG_ROOT", "C:\vcpkg", "Machine")
```

---

## Testing

### Run Tests

```bash
# All tests
cargo test

# Specific test
cargo test --test test_name

# With output
cargo test -- --nocapture
```

### Check Code

```bash
# Check without building
cargo check

# Check all targets
cargo check --all-targets

# Clippy lints
cargo clippy -- -D warnings

# Format code
cargo fmt
```

---

## Performance Tips

### Build Performance

```bash
# Use LLD linker (faster)
# Linux/macOS
cargo install -f cargo-binutils
rustup component add llvm-tools-preview

# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]

# Parallel builds
cargo build -j 8  # Use 8 cores
```

### Runtime Performance

- Use `--release` for production builds
- Consider `--profile release-with-debug` for profiling
- Enable LTO for smaller binaries:

```toml
# Cargo.toml
[profile.release]
lto = true
codegen-units = 1
opt-level = 3
```

---

## Contributing

1. Fork the repository
2. Create feature branch: `git checkout -b feature/my-feature`
3. Make changes and test
4. Commit: `git commit -am 'Add feature'`
5. Push: `git push origin feature/my-feature`
6. Create Pull Request

---

## Resources

### Documentation

- [FFmpeg Documentation]https://ffmpeg.org/documentation.html
- [FFmpeg Wiki]https://trac.ffmpeg.org/wiki
- [Rust FFmpeg Wiki]https://github.com/zmwangx/rust-ffmpeg/wiki

### Tools

- [vcpkg]https://vcpkg.io/
- [Homebrew]https://brew.sh/
- [Chocolatey]https://chocolatey.org/

### Support

- [GitHub Issues]https://github.com/zmwangx/rust-ffmpeg/issues
- [FFmpeg mailing lists]https://ffmpeg.org/contact.html

---

## Quick Reference

### Common Commands

```bash
# Build
cargo build --release

# Run example
cargo run --example video-info -- file.mp4

# Clean build
cargo clean

# Update dependencies
cargo update

# Check for outdated deps
cargo outdated

# Install locally
cargo install --path .
```

### File Locations

- **Source**: `src/`
- **Examples**: `examples/`
- **Tests**: `tests/`
- **Config**: `.cargo/config.toml`
- **Binary**: `target/release/`

### Important Files

- `Cargo.toml` - Dependencies and metadata
- `build.rs` - Build script (vcpkg detection)
- `.cargo/config.toml` - Compiler flags, linker settings
- `GUIDE.md` - User guide with examples
- `DEVELOP.md` - This file

---

**Last Updated:** 2025-11-08
**FFmpeg Version:** 8.0
**Rust Edition:** 2024