Skip to content

Instantly share code, notes, and snippets.

@stayradiated
Last active January 11, 2024 15:33
Show Gist options
  • Save stayradiated/73147ea6383c4f491244f7aeff7ce997 to your computer and use it in GitHub Desktop.
Save stayradiated/73147ea6383c4f491244f7aeff7ce997 to your computer and use it in GitHub Desktop.
Concatenate Videos and Images using ffmpeg
#!/usr/bin/env bash
#
# img2vid
# =======
#
# Convert an image into three second video
#
# Usage: ./img2vid photo.jpg video.mp4
#
# get args
input=$1
output=$2
# create a tmpfile
# TODO: use a different filename each time
tmpdir=/tmp/img2vid/
rm -rf $tmpdir
mkdir -p $tmpdir
# length of video in seconds
length=3
# create a video file from the image
ffmpeg -r 30 -framerate 1/$length -i $input $tmpdir/1.mp4
# add some silence for that professional touch
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i $tmpdir/1.mp4 -shortest -c:v copy -c:a aac $tmpdir/2.mp4
# converts the audio to something sensible
ffmpeg -i $tmpdir/2.mp4 -f lavfi -i aevalsrc=0 -shortest -c:v libx264 -profile:v baseline -crf 23 -c:a aac -strict experimental -vf scale=360:640 $output
#!/usr/bin/env bash
#
# storytime
# =========
#
# Concatenate a folder of images and videos into a single video
#
# Usage: ./storytime mydir
#
# get args
srcdir=$1
# create tmpdir
tmpdir=/tmp/storytime
rm -rf $tmpdir
mkdir -p $tmpdir
# go through each file in the srcdir
for file in $srcdir/*; do
if [ ${file: -5} == ".jpeg" ]; then
# convert images to videos
./img2vid $file "$tmpdir/$(basename $file .jpeg).mp4"
else
# copy videos to tmpdir
cp $file $tmpdir/$(basename $file)
fi
done
# concat all files in tmpdir
./vidcat $tmpdir/*
#!/usr/bin/env bash
#
# vidcat
# ======
#
# Concatenate a list of videos into a single video.
# The final video will be called 'output.mp4'
#
# Usage: ./vidcat one.mp4 two.mp4
#
# remove the last video
rm -rf output.mp4
# create tmp dir to work with
tmpdir=/tmp/vidcat
rm -rf $tmpdir
mkdir -p $tmpdir
# resize all videos and convert them to mpeg
for file in $@; do
out="$tmpdir/$(basename $file .mp4).mpg"
# convert to mpg so we can concat it
ffmpeg -i $file -qscale 0 $out
done
# concat videos and convert to a chrome friendly mp4
cat $tmpdir/*.mpg | ffmpeg -f mpeg -i - -y -strict experimental -acodec aac -ac 2 -ab 160k -vcodec libx264 -s 360:640 -pix_fmt yuv420p -profile:v baseline -level 30 -maxrate 10000000 -bufsize 10000000 -b 1200k -f mp4 -threads 0 output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment