Last active
October 28, 2016 03:58
-
-
Save macfanr/ecf3f73ee804fa0436b71ffd267300d9 to your computer and use it in GitHub Desktop.
video shots with ffmpeg
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/sh | |
function random() | |
{ | |
duration=`./ffmpeg -i "${file}" 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | sed 's@\..*@@g' | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }'` | |
# echo "$duration" | |
min=15 | |
if [ $duration -gt $min ] | |
then | |
r=$RANDOM | |
m=`expr $duration - $min` | |
mr=$(($r%$m)) | |
mr1=`expr $mr + $min` | |
echo $mr1; | |
else | |
mr1=`expr $duration - 1` | |
echo $mr1; | |
fi | |
} | |
if [ $# != 2 ]; then | |
echo "usage: videoshot.sh /Users/danny/MyVideoDir /Users/danny/MyImageDir" | |
exit -1 | |
fi | |
inputDir=$1 | |
outputDir=$2 | |
binaryEXE=./ffmpeg | |
fixImageDir=$outputDir/snaps | |
if [ ! -f "$binaryEXE" ]; then | |
echo "sorry, not found ffmpeg command line tool" | |
exit | |
fi | |
if [ ! -x "$fixImageDir" ]; then | |
mkdir "$fixImageDir" | |
fi | |
for file in $inputDir/*.mp4 | |
do | |
nameNoSuffix=${file%.*} | |
name="${nameNoSuffix##*/}" | |
imageName=$fixImageDir/$name.jpg | |
n=$(random) | |
# echo "./ffmpeg -y -hide_banner -ss $n -i $file -vf scale=400:-1 -frames:v 1 -g 0 -f mjpeg $imageName" | |
./ffmpeg -y -hide_banner -ss $n -i "${file}" -vf scale=400:-1 -frames:v 1 -g 0 -f mjpeg "${imageName}" | |
done; | |
open -R "$fixImageDir" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment