> For the complete documentation index, see [llms.txt](https://harsha-vallamkonda.gitbook.io/harshavallamkonda/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://harsha-vallamkonda.gitbook.io/harshavallamkonda/ffmpeg-commands/ffmpeg-commands-ultimate-guide.md).

# FFmpeg Commands Ultimate Guide

***

#### 1. Installation

**Windows (via winget):**

```bash
winget install ffmpeg
```

**macOS (via Homebrew):**

```bash
brew install ffmpeg
```

**Linux (Ubuntu/Debian):**

```bash
sudo apt update
sudo apt install ffmpeg
```

***

#### 2. Convert Video Format

Convert a video from one container or codec to another without re-encoding (if possible):

```bash
ffmpeg -i input.avi -c copy output.mp4
```

* `-i input.avi`: specifies the input file.
* `-c copy`: copies audio and video streams without re-encoding.

> **Tip:** If `-c copy` fails, re-encode with:
>
> ```bash
> ffmpeg -i input.avi -c:v libx264 -c:a aac output.mp4
> ```

***

#### 3. Compress Video

Reduce file size with controlled quality using CRF (Constant Rate Factor):

```bash
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output_compressed.mp4
```

* `-crf 23`: quality scale (0=lossless, 51=worst). 18–23 is a good range.
* `-preset medium`: tradeoff between speed and compression.
* `-b:a 128k`: sets audio bitrate.

***

#### 4. Trim Video (Extract Segment)

Cut a segment without re-encoding:

```bash
ffmpeg -i input.mp4 -ss 00:01:30 -to 00:03:00 -c copy output_trimmed.mp4
```

* `-ss 00:01:30`: start time at 1 minute 30 seconds.
* `-to 00:03:00`: end time at 3 minutes.
* `-c copy`: stream copy.

> **Note:** For frame-accurate cuts, move `-ss` before `-i` and re-encode.

***

#### 5. Split Video into Segments

Automatically split into equal-length parts with clean keyframe cuts:

```bash
ffmpeg -i input.mp4 -c copy -map 0 \
  -f segment -segment_time 90 \
  -reset_timestamps 1 \
  -force_key_frames "expr:gte(t,n_forced*90)" \
  "segment-%03d.mp4"
```

* `-segment_time 90`: target 90-second segments.
* `-force_key_frames`: ensures keyframes at split points.
* `-reset_timestamps 1`: resets timestamps per segment.

***

#### 6. Merge Videos

Concatenate multiple files without re-encoding:

1. Create `filelist.txt`:

   ```
   file 'part1.mp4'
   file 'part2.mp4'
   file 'part3.mp4'
   ```
2. Run:

   ```bash
   ffmpeg -f concat -safe 0 -i filelist.txt -c copy merged.mp4
   ```

***

#### 7. Resize Video

Change resolution:

```bash
ffmpeg -i input.mp4 -vf "scale=1280:720" output_720p.mp4
```

* `-vf "scale=width:height"`: sets new resolution.

> **Aspect-ratio preservation:** Use `-2` for height or width, e.g., `scale=1280:-2`.

***

#### 8. Crop Video

Crop a region from the video:

```bash
ffmpeg -i input.mp4 -vf "crop=640:480:100:50" cropped.mp4
```

* `crop=out_w:out_h:x:y`.

***

#### 9. Add Watermark

Overlay an image watermark:

```bash
ffmpeg -i input.mp4 -i watermark.png \
  -filter_complex "overlay=10:10" watermarked.mp4
```

* `overlay=10:10`: positions watermark at (10px,10px).

***

#### 10. Apply Filters

**Grayscale:**

```bash
ffmpeg -i input.mp4 -vf "hue=s=0" gray.mp4
```

**Brightness & Contrast:**

```bash
ffmpeg -i input.mp4 -vf "eq=brightness=0.05:contrast=1.2" adjusted.mp4
```

***

#### 11. Create GIF

From a video segment:

```bash
ffmpeg -i input.mp4 -ss 00:00:10 -t 5 \
  -vf "fps=10,scale=320:-1:flags=lanczos" \
  -c:v gif output.gif
```

* `-ss`: start; `-t`: duration.

***

#### 12. Extract Audio

Save only the audio stream:

```bash
ffmpeg -i input.mp4 -vn -c:a libmp3lame -b:a 192k output.mp3
```

* `-vn`: no video.
* `-b:a 192k`: audio bitrate.

***

#### 13. Extract Frames

Capture frames as images:

```bash
ffmpeg -i input.mp4 -vf "fps=1" frame_%04d.png
```

* `fps=1`: one frame per second.

***

#### 14. Generate Thumbnail

Export a single frame:

```bash
ffmpeg -i input.mp4 -ss 00:02:15 -vframes 1 thumbnail.jpg
```

* `-vframes 1`: capture one frame.

***

#### 15. Change Aspect Ratio

Set display aspect ratio:

```bash
ffmpeg -i input.mp4 -vf "scale=640:480,setdar=4:3" output_4_3.mp4
```

***

#### 16. Extract Subtitles

Get subtitles from a video:

```bash
ffmpeg -i input.mkv -map 0:s:0 subs.srt
```

#### 17. Add Subtitles

Embed subtitles into MP4:

```bash
ffmpeg -i input.mp4 -i subs.srt \
  -c:v copy -c:a copy -c:s mov_text output_subbed.mp4
```

***

#### 18. Fix Audio Sync

Delay audio by 0.5s:

```bash
ffmpeg -i input.mp4 -itsoffset 0.5 -i input.mp4 \
  -map 0:v -map 1:a -c copy synced.mp4
```

***

#### 19. Stream to RTMP

Live stream to Twitch/YouTube:

```bash
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac \
  -f flv rtmp://live.twitch.tv/stream_key
```

***

#### 20. Batch Convert Files

Convert all `.avi` files to `.mp4` in a folder (Windows CMD):

```bash
for %i in (*.avi) do ffmpeg -i "%i" -c copy "%~ni.mp4"
```

***

#### 21. Get File Info

Display codec, resolution, duration:

```bash
ffmpeg -i input.mp4
```

***

### Usage Tips

* Surround paths with quotes if they contain spaces.
* For frame-accurate trims, place `-ss` before `-i` and omit `-c copy`.
* Experiment with `-crf` (18–28) and `-preset` (ultrafast–veryslow) for quality vs. speed.
* Consult `ffmpeg -h` for detailed help on flags.

***

*End of FFmpeg Command Reference*


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://harsha-vallamkonda.gitbook.io/harshavallamkonda/ffmpeg-commands/ffmpeg-commands-ultimate-guide.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
