All about FFMPEG, Bento4, Shaka packager (a very fast video and audio converter)
Tags: ffmpeg, video, encoder, decoder
Published: 2023-01-19 09:34:17

### Convert audio or video one format to another 

```bash
ffmpeg -i example.wav example.mp3 #for audio
ffmpeg -i master.mov master.mp4 #for video mov to mp4 format
```
* `-i`: stands for input

#### More complex command
```bash
ffmpeg -i mysong.wav -af loudnorm -c:a mp3
-b:a 256k -ar 48000 mysong.mp3
```
* `-af`: Apply audio filter, [Audio Filter Loudnorm](http://ffmpeg.org/ffmpeg-filters.html#loudnorm): EBU R128 loudness normalization. Includes both dynamic and linear normalization modes. Support for both single pass (livestreams, files) and double pass (files) modes. This algorithm can target IL, LRA, and maximum true peak. In dynamic mode, to accurately detect true peaks, the audio stream will be upsampled to 192 kHz
* `-c:a`: Option for which format we want to export
* `-b:a`: Bitrate of audio
* `-ar`: Audio rate - create an output ﬁle
with a speciﬁc audio sampling rate

#### If we want to cut a portion from a long video with other option we can do that by
```bash
ffmpeg -i https://long_video_link
-t 00:02:30 -c:v h264 -b:v 3M -c:a aac -b:a
128k -ar 44100 -ac 2 example.mp4
```
* `-i`: stands for input
* `-t`: process only for specific duration 
* `-c:v`: video codec `h265 --> mp4`
* `-b:v`: video bitrate
* `-c:a`: audio codec
* `-ar`: Audio rate or sampling rate
* `-ac`: Number audio channels - 2 channels left and right

#### Basic concept about frame
* `I-frame`: Frame stores the whole picture, standard format of FFMPEG
* `P-frame`: Frame stores changes between the current picture and previous ones
* `B-frame`: Frame stores differences with previous or future pictures

#### Convert a video into specific frame rate
```bash
ffmpeg -i YOUR_INPUT -c:v h264 -keyint_min
25 -g 50 -sc_threshold 0 OUTPUT.mp4
```
* `-i`: stands for input
* `-c:v`: video coder
* `-keyint_min`: minimum interval to set a keyframe
* `-g`: Group of pictures, the maximum keyframe interval. In this example- every 50 frames, or 2 seconds, assuming that your input runs at 25 FPS
* `-sc_threshold`: This option will make sure not to
add any new keyframe at scene changes

#### Extract audio from video by Ffmpeg
```bash
ffmpeg -i example.mov -vn -c:a mp3 -b:a
128k -ar 44100 -ac 2 example.mp3
```
* `-vn`: no video 

#### Extract only video by Ffmpeg
```bash
ffmpeg -i example.mp4 -an -c:v copy example.mp4
```
* `-an`: no audio 

#### Different `h264` encoding approaches

**Constant Rate Factor**: which keeps the best quality and care less about the ﬁle size.
`CRF` scale is `0-51`, where 0 is lossless, `23` is default and 51 is worst quality possible. Lower value leads to higher quality. Consider `17 or 18` to be visually lossless or nearly so; it should look the same or nearly the same as the input but it isn't technically lossless

**Presets**: A preset is a collection of options that will provide a certain encoding speed to compression ratio
Available option -
* ultrafast
* superfast
* veryfast
* faster
* fast
* medium 
* slow 
* slower
* veryslow 
* placebo

Using fast saves about 10% encoding time, faster 25%. ultrafast will save 55% at the expense of much lower quality.

**The tune option**
* film – use for high quality movie content
* animation – good for cartoons
* grain – preserves the grain structure in old, grainy film material
* stillimage – good for slideshow-like content
* fastdecode – allows faster decoding by disabling certain filters
* zerolatency – good for fast encoding and low-latency streaming
* psnr – only used for codec development
* ssim – only used for codec development
```bash
ffmpeg -i example.mp4 -c:v h264 -preset ultrafast -crf 0 output.mp4
```

================== Stay tuned for next part =================