Instantly share code, notes, and snippets.
Prepare Video for Byte/IG Story/etc
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 | |
#Quick and dirty way to get a video ready for byte or instagram or other | |
#These are usually in a ratio of 9:16, as opposed to usual viewing of 16:9 | |
#Requires: ffprobe, ffmpeg | |
#Example usages: | |
# 1) | |
# sh prepareVideoForSocialMedia.sh someStream.mp4 00:01:45 6 510 | |
# 2) | |
# sh prepareVideoForSocialMedia.sh someStream.mp4 02:17:00 6 | |
#This is just a helper function for ease of use | |
function convert_seconds_to_hms () { | |
num=$1 | |
min=0 | |
hour=0 | |
sec=0 | |
if((num>59));then | |
((sec=num%60)) | |
((num=num/60)) | |
if((num>59));then | |
((min=num%60)) | |
((num=num/60)) | |
((hour=num)) | |
else | |
((min=num)) | |
fi | |
else | |
((sec=num)) | |
fi | |
#pad zeroes | |
if [[ $hour -lt 10 ]] | |
then | |
hour="0$hour" | |
fi | |
if [[ $min -lt 10 ]] | |
then | |
min="0$min" | |
fi | |
if [[ $sec -lt 10 ]] | |
then | |
sec="0$sec" | |
fi | |
echo "$hour:$min:$sec" | |
} | |
ORIG=$1 #the file you want to extract from | |
START_TIME=$2 #where you want the start. In the format HH:mm:ss | |
LENGTH=$3 #how many seconds it should be (byte is 6, instagram is 30. I think) | |
X_CROP=$4 #the x coordinate you want at the centre of the video (i.e. what you want viewers to see) | |
# If X_CROP is left blank, the video will simply get rotated and scaled to fit | |
LENGTH="$(convert_seconds_to_hms 5)" | |
echo $LENGTH | |
if [ -z $X_CROP] | |
then | |
#Extract segment, rotate, and scale | |
echo "No X-Coordinate provided, rotating and scaling instead of cropping" | |
ffmpeg -i $ORIG -ss $START_TIME -t $LENGTH -filter:v "transpose=0, scale=-1:1080" -c:a copy ${ORIG%.*}-prepared.mp4 | |
else | |
#Extract segment, crop, and scale | |
DIMS=`ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $ORIG` | |
x=${DIMS%x*} | |
y=${DIMS##*x} | |
width=`echo "scale=5;$y / (16 / 9)" | bc` | |
width=`echo $width|cut -f1 -d"."` | |
height=$y | |
echo $width | |
echo $height | |
#This does sort of assume your source video uses a happy audio codec | |
ffmpeg -i $ORIG -ss $START_TIME -t $LENGTH -filter:v "crop=$width:$height:$X_CROP:0, scale=-1:1080" -c:a copy ${ORIG%.*}-prepared.mp4 | |
fi | |
#Explanation of what this script does: | |
# | |
# This script takes a slice of your video, denoted by * | |
# Making it nice for viewing on mobile | |
# | _ _ _ _ _ _ _ _ _ *** _ _ _ _ _ | | |
# | *** | | |
# | *** | | |
# | *** | | |
# | *** | | |
# | *** | | |
# | *** | | |
# | *** | | |
# | _ _ _ _ _ _ _ _ _ *** _ _ _ _ _ | | |
# | |
# Of course, you can just use the rotate option if you want |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment