# flv2mp4
**Repository Path**: vanve/flv2mp4
## Basic Information
- **Project Name**: flv2mp4
- **Description**: No description available
- **Primary Language**: PHP
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2026-08-04
- **Last Updated**: 2026-08-04
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# FLV β MP4 / HLS Conversion Toolkit + H264 Re-encoding Tool
π¨π³ δΈζ β’
π¬π§ English
## Project Overview
A lightweight media processing toolkit implemented in pure PHP 8.1+, **zero external dependencies (no FFmpeg required)**.
Supports FLV, FMP4, MP4, HLS interconversion, live streaming gateway, pushing, pulling, relaying, and **H.264 decoding + scaling + re-encoding** (Baseline Profile).
---
## π Table of Contents
- [Project Overview](#project-overview)
- [Core Features](#-core-features)
- [Requirements](#requirements)
- [Installation](#-installation)
- [Quick Start](#-quick-start)
- [Advanced Features](#-advanced-features)
- [FLV Live Gateway](#flv-live-gateway)
- [Static File Gateway](#static-file-gateway)
- [Push Client](#push-client)
- [Pull Client](#pull-client)
- [Live Relay](#live-relay)
- [Testing & Playback](#-testing--playback)
- [Use Cases](#-use-cases)
- [H.264 Re-encoding Deep Dive](#-h264-decoding--scaling--re-encoding)
- [FLV β HLS Multi-bitrate Example](#flv-hls)
- [FLV β FLV Re-encoding Example](#flv-flv)
- [MP4 β MP4 Re-encoding Example](#mp4-mp4)
- [Watermark Generation Tool](#watermark-generation-tool)
- [Technical Notes](#-technical-notes)
- [License & Disclaimer](#license--disclaimer)
- [Contact](#-contact)
---
## π― Core Features
| Feature | Direction | Description |
|---------|-----------|-------------|
| Container Conversion | FLV β MP4 / FMP4 | Generate standard MP4 or separate fMP4 segments (MSE-compatible) |
| HLS Packaging | FLV β HLS | Generate M3U8 + TS segments, compatible with hls.js, VLC, etc. |
| HLS Reversal | HLS β FLV | Merge HLS segments back into a single FLV file |
| MP4 β FLV | MP4 β FLV / FMP4 β FLV | Multi-container format interconversion |
| Live Gateway | FLV Gateway | High-performance multi-level forwarding with high concurrency support |
| Static File Service | HTTP File Gateway | Lightweight file server with directory listing support |
| Push Client | FLV / MP4 β RTMP/HTTP-FLV/WS-FLV | Push static files as pseudo-live streams |
| Pull Client | RTMP/HTTP-FLV/WS-FLV β FLV | Pull live streams and save as local FLV files |
| Relay Client | Multi-protocol input β Multi-protocol output | One pull, multiple push targets |
| **H.264 Re-encoding** | Decode β Scale β Encode | Baseline Profile support, core engine for multi-bitrate HLS |
---
## Requirements
| Dependency | Description |
|------------|-------------|
| PHP | β₯ 8.1 (**CLI mode only**) |
| `sockets` extension | **Required**, provides underlying Socket communication |
| `gd` extension | **Optional**, used for generating watermarks from PNG/JPG images. If not installed, it will automatically fallback to built-in bitmap font mode. |
π‘ **No FFmpeg, no third-party binaries β all pure PHP.**
---
## π Installation
```bash
composer require xiaosongshu/flv2mp4
```
---
## π Quick Start
```php
debug = true;
$gateway->start();
```
Start the FLV gateway:
```bash
php flvGateway.php
```
### Static File Gateway
Lightweight HTTP file server with directory listing toggle. Create a new file `fileGateway.php` with the following content:
```php
debug = true;
$server->start();
```
Start the file gateway:
```bash
php fileGateway.php
```
### Push Client
Supports HTTP-FLV, WS-FLV, RTMP protocols with speed control and auto-reconnect. Create a new file `pusher.php` with the following content:
```php
require_once __DIR__ . '/vendor/autoload.php';
ini_set('memory_limit', '2048M');
$pusher = new \Xiaosongshu\Flv2mp4\Manage\PusherManage(__DIR__."/test.flv", "http://127.0.0.1:8501/live/stream", 1.0, false);
$pusher->start();
```
Start pushing:
```bash
php pusher.php
```
### Pull Client
Pull live streams and save as local FLV files, suitable for recording or debugging. Create a new file `puller.php` with the following content:
```php
require_once __DIR__ . '/vendor/autoload.php';
ini_set('memory_limit', '2048M');
$puller = new \Xiaosongshu\Flv2mp4\Manage\PullerManage("ws://127.0.0.1:8501/live/stream.flv", __DIR__."/pull_record.flv", 0, false);
$puller->start();
```
Start the pull client:
```bash
php puller.php
```
### Live Relay
Pull one stream and forward to multiple targets simultaneously (supports mixed protocols). Create a new file `forward.php` with the following content:
```php
require_once __DIR__ . '/vendor/autoload.php';
ini_set('memory_limit', '2048M');
$forwarder = new \Xiaosongshu\Flv2mp4\Flv\FlvForwardClient("http://127.0.0.1:8501/a/b.flv", ["rtmp://127.0.0.1:1935/c/d","ws://127.0.0.1:8501/c/e"], 0, true);
$forwarder->start();
```
Start the relay client:
```bash
php forward.php
```
---
## π§ͺ Testing & Playback
| Output Format | Recommended Player | Reference File |
|---------------|--------------------|----------------|
| MP4 | HTML5 `