Skip to content

Instantly share code, notes, and snippets.

@mattandrews
Last active November 20, 2016 14:10
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 mattandrews/aa9d7a645a7359d3eaf0 to your computer and use it in GitHub Desktop.
Save mattandrews/aa9d7a645a7359d3eaf0 to your computer and use it in GitHub Desktop.
Scale, crop and trim videos for Snapchat
#!/bin/bash
# this script does four things:
#
# 1. detects any crop marks (black bars) on the source clip
# 2. uses those crop marks to trim the clip so it has no bars (semi-manual task)
# 3. scales the video up ands crops it (from the center) to snapchat size
# 4. trims the clip to 10s long (might make sense to do this step first?)
INPUT='vid.mp4'
DUMMY='dummy.avi'
TMP='tmp.mp4'
CROPPED='cropped.mp4'
OUTPUT='out.mp4'
DESIRED_WIDTH=1080
DESIRED_HEIGHT=1920
START_TIMECODE=00:00:00.0
CLIP_LENGTH=00:00:10.0
# 1. detect the crop marks (numbers here are tolerance levels for detecting black bars)
ffmpeg -i $INPUT -vf "cropdetect=24:16:0" $DUMMY
printf "
######################
What was the crop?
For example, for this output:
[Parsed_cropdetect_0 @ 0x7fca9bc214a0] x1:0 x2:639 y1:0 y2:359 w:640 h:352 x:0 y:4 pts:51984 t:86.640000 crop=640:352:0:4
the crop value is:
640:352:0:4
######################
Type the crop value and press Enter: "
read CROP_VALUES
# 2. crop the video to cut out those black bars:
ffmpeg -i $INPUT -filter:v "crop=$CROP_VALUES" -c:a copy $TMP
# 3. now scale it up and crop it to correct size
ffmpeg -i $TMP -filter:v "scale=iw*$DESIRED_HEIGHT/ih:$DESIRED_HEIGHT, crop=$DESIRED_WIDTH:$DESIRED_HEIGHT" -c:a copy $CROPPED
# 4. finally, trim the clip to the desired length
ffmpeg -i $CROPPED -ss $START_TIMECODE -t $CLIP_LENGTH $OUTPUT
# cleanup tmp files
rm $DUMMY $TMP $CROPPED
@mbarghi
Copy link

mbarghi commented Nov 20, 2016

how do i run this script?

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