Last active
March 30, 2021 14:41
-
-
Save 9sako6/06cb448aa211c0b0014a96bf4db0796e to your computer and use it in GitHub Desktop.
generate_video_preview.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -u | |
source_file_name=${1} | |
output_file_name="preview_$(date "+%Y%d%m%H%M%S").mp4" | |
preview_tmp_dir=preview_tmp | |
split_number=${2} | |
numberings=$(seq 0 `expr ${split_number} \- 1`) | |
video_duration_ms=$(printf '%.0f\n' $(ffprobe -show_streams -print_format json ${source_file_name} 2>/dev/null | awk -F\" '/"duration"/{print $4}' | head -n 1)) | |
mkdir -p ${preview_tmp_dir} | |
# Generate 1 sec preveiw videos | |
for i in $(echo ${numberings} | xargs -n1 echo); do | |
start_ms=`expr ${video_duration_ms} \/ ${split_number} \* ${i}` | |
end_ms=`expr ${start_ms} \+ 1` | |
ffmpeg -ss ${start_ms} -to ${end_ms} -i "${source_file_name}" "${preview_tmp_dir}/preview${i}.mp4" | |
done | |
# Concatenate preview videos | |
ffmpeg -f concat -safe 0 -i <(echo ${numberings} | xargs -n1 -I{} echo "file ${PWD}/${preview_tmp_dir}/preview{}.mp4") "${output_file_name}" | |
rm -rf "${preview_tmp_dir}" | |
echo ${output_file_name} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
It generates a 5-second preview video in current directory.