Hls and Dash packaging simultaneously with Ffmpeg and Shaka packager
Tags: video, stream, ffmpeg, shaka
Published: 2023-01-28 11:04:35

**Dash** is the open-source equivalent of Apple's HLS technology, used for the same purpose: to deliver live videos or on-demand videos over the web, dynamically changing the quality based on the internet connection of the ﬁnal user.

**HlS** it's a standard created by apple in order to deliver audio/video streaming using ordinary web servers.

Following formula will produce a multi-bit-rate version of our master with h264 constrained encoding

```bash
ffmpeg -i input.mp4 -c:v h264 -crf 22 -tune film -profile:v main -level:v 4.0 -maxrate 5000k -bufsize 10000k -r 25 -keyint_min 25 -g 50 -sc_threshold 0 -c:a aac -ar 44100 -b:a 128k -ac 2 -pix_fmt yuv420p -movflags +faststart 1080.mp4 -s 1280x720 -c:v h264 -crf 24 -tune film -profile:v main -level:v 4.0 -maxrate 2500k -bufsize 5000k -r 25 -keyint_min 25 -g 50 -sc_threshold 0 -c:a aac -ar 44100 -b:a 128k -ac 2 -pix_fmt yuv420p -movflags +faststart 720.mp4 -s 854x480 -c:v h264 -crf 30 -tune film -profile:v main -level:v 4.0 -maxrate 1250k -bufsize 2500k -r 25 -keyint_min 25 -g 50 -sc_threshold 0 -c:a aac -ar 44100 -b:a 96k -ac 2 -pix_fmt yuv420p -movflags +faststart 480.mp4 -s 640x360 -c:v h264 -crf 33 -tune film -profile:v main -level:v 4.0 -maxrate 900k -bufsize 1800k -r 25 -keyint_min 25 -g 50 -sc_threshold 0 -c:a aac -ar 44100 -b:a 96k -ac 2 -pix_fmt yuv420p -movflags +faststart 360.mp4 -s 320x24 -c:v h264 -crf 36 -tune film -profile:v main -level:v 4.0 -maxrate 625k -bufsize 1250k -r 25 -keyint_min 25 -g 50 -sc_threshold 0 -c:a aac -ar 22050 -b:a 64k -ac 1 -pix_fmt yuv420p -movflags +faststart 240.mp4
```
> All tags described in my previous blog

#### Create multi-bitrate DASH and HLS playlist with **Shaka packager**

Shaka packager official documentation, [check_out](https://shaka-project.github.io/shaka-packager/html/tutorials/tutorials.html) installation and user guide from their official doc

```bash
./packager-linux-x64 in=1080.mp4,stream=audio,output=audio.mp4,playlist_name=audio.m3u8,hls_group_id=audio,hls_name=ENGLISH \
                                          in=1080.mp4,stream=video,output=h264_1080.mp4,playlist_name=h264_1080p.m3u8,iframe_playlist_name=h264_1080p_iframe.m3u8 \
                                          in=480.mp4,stream=video,output=h264_480p.mp4,playlist_name=h264_480p.m3u8,iframe_playlist_name=h264_480p_iframe.m3u8 \
                                          in=360.mp4,stream=video,output=h264_360p.mp4,playlist_name=h264_360p.m3u8,iframe_playlist_name=h264_360p_iframe.m3u8 \
                                          in=240.mp4,stream=video,output=h264_240p.mp4,playlist_name=h264_240p.m3u8,iframe_playlist_name=h264_240p_iframe.m3u8 \
                                          --hls_master_playlist_output h264_master.m3u8 \
                                          --mpd_output h264.mpd
```

#### Explanation
`in` : `in=your_input_video.mp4`, input command
`stream` : `stream=audio` for extract only audio file from input file and `stream=video` for extract only video file from input file

Previously we got different chunk file for different resolution from `FFmpeg` previous command like `1080.mp4`,`720.mp4`,`480.mp4` etc.

For making playlist we have to put respected file one by one 
```bash
  in=1080.mp4,stream=video,output=h264_1080.mp4,playlist_name=h264_1080p.m3u8,iframe_playlist_name=h264_1080p_iframe.m3u8 \
                                          in=480.mp4,stream=video,output=h264_480p.mp4,playlist_name=h264_480p.m3u8,iframe_playlist_name=h264_480p_iframe.m3u8 \
                                          in=360.mp4,stream=video,output=h264_360p.mp4,playlist_name=h264_360p.m3u8,iframe_playlist_name=h264_360p_iframe.m3u8 \
                                          in=240.mp4,stream=video,output=h264_240p.mp4,playlist_name=h264_240p.m3u8,iframe_playlist_name=h264_240p_iframe.m3u8 \
```
`--hls_master_playlist_output`: Generator master playlist for hls streaming 
`--mpd_output h264.mpd`: Generator master playlist for dash streaming