# Video Format Conversion Setup

## Overview
The system automatically converts AVI, MOV, WMV, MKV, FLV, and WebM files to MP4 format after upload **if FFmpeg is available**. If FFmpeg is not installed, the original file is kept without errors.

## ✅ FFmpeg is OPTIONAL

- If FFmpeg installed → AVI converted to MP4 ✓
- If FFmpeg not installed → Original AVI kept (no errors) ✓
- Works either way! 🎉

## Installation (Optional - Only if you want conversion)

### Ubuntu/Debian
```bash
sudo apt-get update
sudo apt-get install ffmpeg

# Verify
ffmpeg -version
```

### macOS
```bash
brew install ffmpeg
ffmpeg -version
```

### Windows (Laragon)
Download from https://ffmpeg.org/download.html and add to PATH

## How It Works

```
User uploads AVI file (1GB)
    ↓
JavaScript splits into 5MB chunks
    ↓
All chunks upload via AJAX
    ↓
Backend merges chunks into single AVI file
    ↓
System checks: Is FFmpeg available?
    ├─→ YES: Convert AVI → MP4
    └─→ NO: Keep original AVI
    ↓
File saved to database
```

## Features

✅ **Optional FFmpeg** - Works with or without it  
✅ **Graceful fallback** - If FFmpeg unavailable, uses original file  
✅ **No errors** - System doesn't break if conversion can't happen  
✅ **Automatic detection** - Checks for FFmpeg on each conversion attempt  
✅ **Logging** - All conversions logged to `storage/logs/laravel.log`

## Supported Formats for Conversion

| Input Format | Output | Codec |
|---|---|---|
| AVI | MP4 | H.264 |
| MOV | MP4 | H.264 |
| WMV | MP4 | H.264 |
| MKV | MP4 | H.264 |
| FLV | MP4 | H.264 |
| WebM | MP4 | H.264 |
| MP4 | MP4 | (skipped - already MP4) |

## Configuration

To enable video conversion, install FFmpeg as shown in Installation section.

To adjust conversion settings if using FFmpeg, edit `app/Services/VideoConversionService.php`:

```php
// Line around 50: Adjust encoding preset
'-preset fast'     // Options: ultrafast, fast, medium, slow
```

Preset speeds (faster = lower quality):
- `ultrafast` - Fastest, lowest quality
- `fast` - Recommended (default)
- `medium` - Balanced
- `slow` - Best quality, slowest

## Troubleshooting

### Videos uploading without conversion? (No FFmpeg)

Check if FFmpeg is available:
```bash
ffmpeg -version
```

If not found and you want conversion, install it (see Installation section above).

**But don't worry!** Videos upload fine without FFmpeg - just no format conversion.

### Want to verify FFmpeg is installed?

```bash
# Test FFmpeg
php artisan tinker
> shell_exec('ffmpeg -version');
```

If it returns version info, conversion will work! ✓

### Conversion is slow?

Change preset in code from `fast` to `ultrafast`:
```php
'-preset ultrafast'
```

## Monitoring Conversions

Check conversion logs:
```bash
# View recent conversions
tail -f storage/logs/laravel.log | grep -i "conversion\|ffmpeg"

# Find all conversion activities  
grep -i "conversion" storage/logs/laravel.log
```

## Performance Tips

1. **No FFmpeg needed** - Videos upload and work either way
2. **Want fast conversion?** - Adjust preset from `fast` to `ultrafast`
3. **Monitor disk space** - Keep 20-30% free space on storage drive
4. **Optional feature** - Don't install FFmpeg if not needed

## Error Handling

If FFmpeg is not available:
1. System detects FFmpeg is missing ✓
2. Logs a warning message ✓
3. Keeps original file format ✓
4. User can still save content ✓
5. No errors shown to user ✓

## Files Modified

- `app/Services/VideoConversionService.php` - Checks for FFmpeg availability, graceful fallback
- `app/Http/Controllers/Backend/AdminNew/ChunkUploadController.php` - Calls conversion service
- `resources/views/livewire/backend/admin_new/content-creator/create.blade.php` - Shows conversion message when it happens
- `public/js/chunked-uploader.js` - Handles response data
