Skip to content

Instantly share code, notes, and snippets.

@9sako6
Last active March 30, 2021 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9sako6/06cb448aa211c0b0014a96bf4db0796e to your computer and use it in GitHub Desktop.
Save 9sako6/06cb448aa211c0b0014a96bf4db0796e to your computer and use it in GitHub Desktop.
generate_video_preview.sh
#!/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}
@9sako6
Copy link
Author

9sako6 commented Mar 30, 2021

Usage

It generates a 5-second preview video in current directory.

$ ./generate_video_preview.sh input.mp4 5

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