Skip to content

Instantly share code, notes, and snippets.

@eviltester
Last active September 28, 2023 15:12
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save eviltester/7beef92896fdd8b638656f996fac38c0 to your computer and use it in GitHub Desktop.
Save eviltester/7beef92896fdd8b638656f996fac38c0 to your computer and use it in GitHub Desktop.
Convert videos into subtitled sections using ffmpeg
# Create a new caption file
~~~~~~~~
ffmpeg -i captions.srt captions.ass
~~~~~~~~
# Add subtitles to main video without changing it
~~~~~~~~
ffmpeg -i video.mp4 -vf "subtitles=captions.ass:force_style='OutlineColour=&H80000000,BorderStyle=4,Outline=1,Shadow=0,MarginV=20'" subtitled-video.mp4
~~~~~~~~
# Change size of video to support Instagram
~~~~~~~~
ffmpeg -i video.mp4 -vf scale=720:720:force_original_aspect_ratio=decrease,pad=720:720 instagram-sized.mp4
~~~~~~~~
# Add subtitles to the instagram video
~~~~~~~~
ffmpeg -i instagram-sized.mp4 -vf "subtitles=captions.ass:force_style='OutlineColour=&H80000000,BorderStyle=4,Outline=1,Shadow=0,MarginV=90'" instagram-subs.mp4
~~~~~~~~
# Split the Instagram Subtitled video into 3 new videos
~~~~~~~~
ffmpeg -i instagram-subs.mp4 -ss 00:01:05 -t 00:00:52 instagram-01-05.mp4
~~~~~~~~
~~~~~~~~
ffmpeg -i subtitled-video.mp4 -ss 00:01:05 -t 00:00:52 subtitled-01-05.mp4
~~~~~~~~
Repeat as necessary using different videos and different sections
# To format for IGTV
The command below uses a headerimage.png which is 720x426
~~~~~~~~
ffmpeg -i instagram-subs.mp4 -i headerimage.png -filter_complex "scale=w=720:h=1280:force_original_aspect_ratio=1,pad=720:1280:(ow-iw)/2:426,overlay" instagram-subs-igt.mp4
~~~~~~~~
I looked through the video to identify the spots I wanted to split the video:
- 00- 09:29 - intro and examples
- 09:29 - 15:56 process workarounds
- 15:58 - 20:45 (end) testing, automating and mindset
And that gave me the following 'split' ffmpeg commands. Remember the second time is a duration i.e. start at 00:00 for duraton 09:29
~~~~~~~~
ffmpeg -i instagram-subs-igt.mp4 -ss 00:00:00 -t 00:09:29 igt-podcast-006-part1.mp4
ffmpeg -i instagram-subs-igt.mp4 -ss 00:09:29 -t 00:06:28 igt-podcast-006-part2.mp4
ffmpeg -i instagram-subs-igt.mp4 -ss 00:15:57 -t 00:10:00 igt-podcast-006-part3.mp4
~~~~~~~~
# Add an image after the video
I want to add an image for 5 seconds, or however long, after the main mp4.
To do that, I creat an mp4 which is just the image and I concatenate it to the main file.
~~~~~~~~
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -loop 1 -i image.png -pix_fmt yuv420p -t 5 -vf scale=1920:1080 promo-image-w-audio.mp4
~~~~~~~~
Important points for the above are:
- I'm scaling the image as I create the video, so that the output file has the same resolution as the one I want to append it to.
- `-vf scale=1920:1080`
- I add a silent audio layer to allow the concatenation to work
- `-f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100
Then I need to concatenate the videos together:
~~~~~~~~
ffmpeg -i video.mp4 -i promo-image-w-audio.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4
~~~~~~~~
@eviltester
Copy link
Author

eviltester commented Sep 9, 2018

Create a new caption file

ffmpeg -i captions.srt captions.ass

Add subtitles to main video without changing it

ffmpeg -i video.mp4  -vf "subtitles=captions.ass:force_style='OutlineColour=&H80000000,BorderStyle=4,Outline=1,Shadow=0,MarginV=20'" subtitled-video.mp4

Change size of video to support Instagram

ffmpeg -i video.mp4 -vf scale=720:720:force_original_aspect_ratio=decrease,pad=720:720 instagram-sized.mp4

Add subtitles to the instagram video

ffmpeg -i instagram-sized.mp4 -vf "subtitles=captions.ass:force_style='OutlineColour=&H80000000,BorderStyle=4,Outline=1,Shadow=0,MarginV=90'" instagram-subs.mp4

Split the Instagram Subtitled video into 3 new videos

ffmpeg -i instagram-subs.mp4 -ss 00:01:05 -t 00:00:52 instagram-01-05.mp4
ffmpeg -i subtitled-video.mp4 -ss 00:01:05 -t 00:00:52 subtitled-01-05.mp4

Repeat as necessary using different videos and different sections

To format for IGTV

The command below uses a headerimage.png which is 720x426

ffmpeg -i instagram-subs.mp4 -i headerimage.png -filter_complex "scale=w=720:h=1280:force_original_aspect_ratio=1,pad=720:1280:(ow-iw)/2:426,ov

I looked through the video to identify the spots I wanted to split the video:

  • 00- 09:29 - intro and examples
  • 09:29 - 15:56 process workarounds
  • 15:58 - 20:45 (end) testing, automating and mindset

And that gave me the following 'split' ffmpeg commands. Remember the second time is a duration i.e. start at 00:00 for duraton 09:29

ffmpeg -i instagram-subs-igt.mp4 -ss 00:00:00 -t 00:09:29 igt-podcast-006-part1.mp4
ffmpeg -i instagram-subs-igt.mp4 -ss 00:09:29 -t 00:06:28 igt-podcast-006-part2.mp4
ffmpeg -i instagram-subs-igt.mp4 -ss 00:15:57 -t 00:10:00 igt-podcast-006-part3.mp4

Add an image after the video

I want to add an image for 5 seconds, or however long, after the main mp4.

To do that, I creat an mp4 which is just the image and I concatenate it to the main file.

ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -loop 1 -i image.png -pix_fmt yuv420p -t 5 -vf scale=1920:1080  promo-image-w-audio.mp4

Important points for the above are:

  • I'm scaling the image as I create the video, so that the output file has the same resolution as the one I want to append it to.
    • -vf scale=1920:1080
  • I add a silent audio layer to allow the concatenation to work
    • `-f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100

Then I need to concatenate the videos together:

ffmpeg -i video.mp4 -i promo-image-w-audio.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4

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