Skip to content

Instantly share code, notes, and snippets.

@Postrediori
Last active December 28, 2019 16:56
Show Gist options
  • Save Postrediori/1c4d1ae244d92b177caf1e25a98c2cab to your computer and use it in GitHub Desktop.
Save Postrediori/1c4d1ae244d92b177caf1e25a98c2cab to your computer and use it in GitHub Desktop.
Script for downloading a video with orbits of Jupiter&asteroids and fixing rotation on Jupiter
#!/bin/bash
#
# Download Jupiter/asteroids video and fix Jupiter's rotation
#
# Tweet address with source video:
# https://mobile.twitter.com/AntonioParis/status/994224252961386496
#
# Required utils:
# * curl
# * convert
# * ffmpeg
#
#
# Script parameters
#
video_url=https://video.twimg.com/tweet_video/Dcwxr0SVMAI6RhJ.mp4
source_video=Jupiter.mp4
dest_video=JupiterRotated.mp4
ffmpeg_log=ffmpeg.log
#
# Download source video
#
echo -n Downloading video file from URL ${video_url}
curl ${video_url} --output ${source_video} --silent
if [[ $? -ne 0 ]]; then
echo ...Failed
echo Error: Could not download video file
exit 1
fi
echo ...Ok
#
# Clean temporary directory for original frames
#
if [[ -d frames ]]; then
rm -r frames
fi
mkdir frames
#
# Extract frames from source video
#
echo -n Extracting frames from video ${source_video}
ffmpeg -i ${source_video} frames/out%03d.png 2> ${ffmpeg_log}
if [[ $? -ne 0 ]]; then
echo ...Failed
echo Error: Unable to extract frames from video ${source_video}
cat ${ffmpeg_log}
exit 1
fi
echo ...Ok
#
# Clean temporary directory for processed frames
#
if [[ -d outframes ]]; then
rm -r outframes
fi
mkdir outframes
#
# Rotate video frames
#
counter=0
frame_count=$( ls -1q frames/*.png | wc -l )
for file in frames/*.png; do
echo -n Process frame ${counter} of ${frame_count}
frame_name="$( basename "${file}" )"
convert "$file" \
-background black \
-rotate "$( echo "1.60 * $counter" | bc )" \
-gravity center \
-extent 710x710 \
outframes/${frame_name}
if [[ $? -ne 0 ]]; then
echo ...Failed
echo Error: Unable to process frame file ${file}
exit 1
fi
echo ...Ok
counter=$(( $counter + 1 ))
done
#
# Create video file from rotated frames
#
echo -n Create video ${dest_video}
ffmpeg \
-framerate 30 \
-start_number 0 \
-i outframes/out%03d.png \
-c:v libx264 -r 30 -pix_fmt yuv420p \
-y \
${dest_video} 2> ${ffmpeg_log}
if [[ $? -ne 0 ]]; then
echo ...Failed
echo Error: Unable to create video ${dest_video}
cat ${ffmpeg_log}
exit 1
fi
echo ...Ok
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment