Skip to content

Instantly share code, notes, and snippets.

@webaware
Created June 9, 2022 08:37
Show Gist options
  • Save webaware/6b628a59f9e5ad6fa2b979a2f0a44b54 to your computer and use it in GitHub Desktop.
Save webaware/6b628a59f9e5ad6fa2b979a2f0a44b54 to your computer and use it in GitHub Desktop.
Build web-ready videos from .mov and .mp4 sources
SRC := Brightscape.mov FUNK-instagram-mockup.mp4
SRC_MOV := $(filter %.mov,$(SRC))
SRC_MP4 := $(filter %.mp4,$(SRC))
JPG := $(patsubst %.mov, out/%-vid.jpg, $(SRC_MOV)) $(patsubst %.mp4, out/%-vid.jpg, $(SRC_MP4))
MP4 := $(patsubst %.mov, out/%.mp4, $(SRC_MOV)) $(patsubst %.mp4, out/%.mp4, $(SRC_MP4))
VP9 := $(patsubst %.mov, out/%.webm, $(SRC_MOV)) $(patsubst %.mp4, out/%.webm, $(SRC_MP4))
.PHONY: all jpg mp4 vp9
all: jpg mp4 vp9
jpg: $(JPG)
mp4: $(MP4)
vp9: $(VP9)
clean:
rm -f out/* ffmpeg2pass-0.log*
# Extract a frame as a still image
out/%-vid.jpg: %.*
$(eval TMP_JPG := $(shell mktemp tmp.XXXXXXXXXX.jpg))
ffmpeg -y -hide_banner -accurate_seek -ss 00:01 -i "$<" -vf "scale='min(1920,iw)':-2" -q:v 1 -frames:v 1 $(TMP_JPG)
mozjpeg -optimize -quality 85 -outfile $@ $(TMP_JPG)
rm -f $(TMP_JPG)
# Convert to mp4
# NB: iPhone wants <= 1920 wide and yuv420p (maybe some others, but these work)
out/%.mp4: %.*
rm -f $@
ffmpeg -hide_banner -threads 4 -i "$<" -vf "scale='min(1920,iw)':-2" -pix_fmt yuv420p -c:v libx264 -b:v 2600k -pass 1 -an -f null /dev/null
ffmpeg -hide_banner -threads 4 -i "$<" -vf "scale='min(1920,iw)':-2" -pix_fmt yuv420p -c:v libx264 -b:v 2600k -pass 2 -c:a aac -ac 2 -b:a 128k $@
## Convert to webm
out/%.webm: %.*
rm -f $@
ffmpeg -hide_banner -threads 4 -i "$<" -vf "scale='min(1920,iw)':-2" -c:v libvpx-vp9 -b:v 1800k -crf 10 -pass 1 -an -f null /dev/null
ffmpeg -hide_banner -threads 4 -i "$<" -vf "scale='min(1920,iw)':-2" -c:v libvpx-vp9 -b:v 1800k -crf 10 -pass 2 -c:a libopus -ac 2 -b:a 128k $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment