Skip to content

Instantly share code, notes, and snippets.

@x1wins
Last active Apr 16, 2019
Embed
What would you like to do?
FFMpeg convert ts to hls
#!/usr/bin/env bash
set -e
# Usage create-vod-hls.sh SOURCE_FILE [OUTPUT_NAME]
[[ ! "${1}" ]] && echo "Usage: create-vod-hls.sh SOURCE_FILE [OUTPUT_NAME]" && exit 1
source="${1}"
target="${2}"
command="ffmpeg -hide_banner -y -i ${1} \
-vf scale=w=640:h=360 -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 ${target}/360p_%03d.ts ${target}/360p.m3u8 \
-vf scale=w=842:h=480 -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 ${target}/480p_%03d.ts ${target}/480p.m3u8 \
-vf scale=w=1280:h=720 -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 ${target}/720p_%03d.ts ${target}/720p.m3u8 \
-vf scale=w=1920:h=1080 -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 ${target}/1080p_%03d.ts ${target}/1080p.m3u8"
echo mkdir ${target}
mkdir ${target}
echo ${command}
echo "Encoding Start"
date
${command}
echo copy playlist to ./${target}/
cp playlist.m3u8 ./${target}/
echo "Encoding Complete"
date
@x1wins

This comment has been minimized.

Copy link
Owner Author

@x1wins x1wins commented Mar 9, 2019

How to run

  1. download playlist.m3u8 to same path of encoding.sh
    https://gist.github.com/9a7214a203ca1aa5a3c424cd0f6971f8.git

  2. bash run

# bash encoding_h264.sh SOURCE_FILE SAVE_PATH
$ bash encoding_h264.sh out.mp4 movie1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment