Created
November 3, 2017 15:20
-
-
Save lukereding/86959bc188da263d0fb8ff72f8b506b2 to your computer and use it in GitHub Desktop.
bash script to download an animation and create video with variable speeds
This file contains hidden or 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
# the goal: | |
## download the animation | |
## use ffmpeg to make it playable | |
## change the speed of the animation | |
## randomally change the speed of the animation | |
## concatenate multiple speeds together | |
# make a directory to keep things tidy | |
mkdir create_video | |
cd create_video | |
# grab the animation from the web, renaming it to original_video.mp4 | |
wget https://ndownloader.figshare.com/files/8804191 -O original_video | |
# make the video actually about to be viewed, but only take the first 10 seconds | |
ffmpeg -t 00:00:10 -i original_video -vcodec libx264 original_video_corrected.mp4 | |
# a quick google search reveals how to use ffmpeg to change the speed of a video: | |
## https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video | |
## values < 1 speed it up; > 1 slow it down | |
## possible speeds: | |
SPEEDS=(0.5 0.75 1.0 1.33 2.0 3.0 0.1 0.2) | |
echo $SPEEDS | |
# make a video for each possible speed | |
for i in ${SPEEDS[@]}; | |
do | |
echo "making video for speed $i" | |
ffmpeg -i original_video_corrected.mp4 -filter:v "setpts=$i*PTS" -y "speed_$i.mp4" | |
done | |
# for this example, say we want to stitch together 10 videos | |
# this code block creates a list of randomized speeds | |
# the available speeds are defined from the SPEEDS variable | |
# at the end of this we have an array (a list of numbers) called RANDOM_LIST | |
# that contains the list of randomized speeds | |
for i in `seq 1 10`; | |
do | |
echo randomization: $i # print loop # to the screen | |
rand=$[ $RANDOM % ${#SPEEDS[@]} ] # generates random number from 1-5 | |
RANDOM_SPEED=${SPEEDS[$rand]} # chooses the rand'th speed from SPEED | |
RANDOM_LIST[$i]=$RANDOM_SPEED # add random speed to RANDOM_LIST | |
done | |
# this create a plain text file, list, that lists the videos we want to stitch together | |
# based on out randomized list of speeds | |
for i in ${RANDOM_LIST[@]}; | |
do | |
printf "file '%s'\n" speed_$i.mp4 >> list | |
done | |
# this concatencates all the videos together | |
ffmpeg -f concat -i list result.mp4 | |
# this opens the final resulting video | |
open result.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment