Skip to content

Instantly share code, notes, and snippets.

@Loopshape
Created November 26, 2018 08:31
Show Gist options
  • Save Loopshape/ff5b679b9acfe73420993d306c83204c to your computer and use it in GitHub Desktop.
Save Loopshape/ff5b679b9acfe73420993d306c83204c to your computer and use it in GitHub Desktop.
Splitting, cropping and converting video for Instagram

Short description

Just a short reminder to self, when i'll be struggling with uploading video to Instagram. For some reason my MIUI v5 port has troubles with recording proper video to be successfully uploaded to Instagram.

First of all: encoding should be mpeg4, not h264!, regardless of file extension (but should probably be mp4).

All the options passed to ffmpeg are order dependant: first options executed first, last will override previous. Thus, input file is defined early, output file is defined at the very end.

All filters can be setup per stream (a for audio and v for video) and should be defined as -filter (for both), -filter:a (for audio) or -filter:v (for video). The -vf is an alias for -filter:v.

Actual FFmpeg shell commands

Crop is defined as frame_width:frame_height:x_offset:y_offset. The framerate and qscale are probably not significant here.

ffmpeg -threads 2 -i input.3gp -vf crop=720:720:0:0 -framerate 30 -strict experimental -qscale 0 cropped-square.mp4

Cut required video clips. -ss defines start time, -t defines duration, -to defines stop time (-t and -to are mutually exclusive, -t has priority). All accept time in hh:mm:ss.msc format, but milliseconds part can be ommited. All of these should be defined before -i option, otherwise there will be weird results.

ffmpeg -threads 2 -ss 00:01:20 -t 00:00:14 -i cropped-square.mp4 -vcodec copy -acodec copy cut1.mp4

Before glueing mp4 files they should be loslessly and quickly converted to TS.

ffmpeg -i cut1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts

Merge TS files using concatenation protocol. Drop audio with -an option, slow down the video with -vf "setpts=0.25*PTS". The PTS ratio is inverted value to regular video speed. 100% (normal speed) is 1 , 400% is 0.25, 50% is 2. When speeding up video it's better when input video stream has more FPS than output will have, since speeding up will drop frames.

ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -vf "setpts=0.25*PTS" -an output.mp4

If the final output is too long after merging all clips you can repeat speedup and clipping.

ffmpeg -i output.mp4 -framerate 30 -vf "setpts=0.75*PTS" -ss 00:00:00 -t 00:00:15 output-15-fr30.mp4

The final part, very important - transcoding into mpeg4. video bitrate can be anything you want.

ffmpeg -i output-15-fr30.mp4 -vcodec mpeg4 -vb 8000k -strict experimental -qscale 0 output-mpeg4.mp4

Points of improvement

  1. One should probably want to split long video into clips before doing any heavylifting converting/cropping
  2. Assure video has 30 FPS before any slowdowns
  3. Probably merge some steps together
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment