Skip to content

Instantly share code, notes, and snippets.

@tuhinpal
Last active April 16, 2021 22:23
Show Gist options
  • Save tuhinpal/35d703ee8d1d76f40aa8015d9df5be6f to your computer and use it in GitHub Desktop.
Save tuhinpal/35d703ee8d1d76f40aa8015d9df5be6f to your computer and use it in GitHub Desktop.
Convert video to hls
echo "Enter input video path with extension (Ex. input.mp4)"
read input_path
echo "Enter output folder (Ex. output)"
read output_path
mkdir $output_path
ffmpeg -hide_banner -y -i $input_path \
-vf scale=w=640:h=360:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_segment_filename $output_path/360p_%03d.ts $output_path/360p.m3u8 \
-vf scale=w=842:h=480:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 1400k -maxrate 1498k -bufsize 2100k -b:a 128k -hls_segment_filename $output_path/480p_%03d.ts $output_path/480p.m3u8 \
-vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -hls_segment_filename $output_path/720p_%03d.ts $output_path/720p.m3u8 \
-vf scale=w=1920:h=1080:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 5000k -maxrate 5350k -bufsize 7500k -b:a 192k -hls_segment_filename $output_path/1080p_%03d.ts $output_path/1080p.m3u8
cd $output_path
echo '#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=640x360
360p.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1400000,RESOLUTION=842x480
480p.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1280x720
720p.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080
1080p.m3u8' > master.m3u8
echo "Done. Please check $output_path/master.m3u8"
@tuhinpal
Copy link
Author

tuhinpal commented Apr 8, 2021

Instruction

  • Use debian or ubuntu (Tested)
  • Install ffmpeg
    apt install ffmpeg -y
    
  • Run this and follow on screen instructions
    wget https://gist.github.com/cachecleanerjeet/35d703ee8d1d76f40aa8015d9df5be6f/raw/75b1b56b018b1a7f8fac1ddddac5975dba114a5a/vod-converter.sh
    
    bash vod-converter.sh
    

Output quality

  • 360p
  • 480p
  • 720p
  • 1080p

Output filename

master.m3u8

Credits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment