FFMpeg must be installed.
Instructions:
- On Mac: Use Homebrew
brew install ffmpeg
- http://ffmpeg.org/download.html
- Input video: MP4 in H.264 or HEVC/H.265
- Captions file: SRT or VTT/WebVTT
Note: If you do not have a captions file upload your MP4 video to YouTube and generate automated captions. Then download the SRT file. Alternatively, if you have a Microsoft Stream subscription upload the video and download the WebVTT captions file.
These subtitles can be toggled on or off. You can even have multiple subtitlte tracks for different languages or commentary.
# Replace with your input video and subtitle file
INFILE=~/Desktop/deployingwebapps.mp4
SUBTITLES=~/Desktop/deployingwebapps.vtt
ffmpeg -i $INFILE -i $SUBTITLES -c copy -c:s mov_text -metadata:s:s:0 language=eng outfile_selectable.mp4
# Replace with your input video and subtitle files
INFILE=~/Desktop/deployingwebapps.mp4
SUBTITLESENG=~/Desktop/english.vtt
SUBTITLEGER=~/Desktop/german.vtt
SUBTITLESSPA=~/Desktop/spanish.vtt
ffmpeg -i $INFILE -i $SUBTITLESENG -i $SUBTITLESGER -i $SUBTITLESSPA -map 0 -map 1:s -map 2:s -map 3:s -c copy -c:s mov_text -c:s mov_text c:s mov_text -metadata:s:s:0 language=eng -metadata:s:s:1 language=ger -metadata:s:s:2 language=spa outfile_selectable_multi.mp4
Since we need to rerender the video we use hardware acceleration.
For H.264 with hardware encoding on Mac
# Replace with your input video and subtitle file
INFILE=~/Desktop/deployingwebapps.mp4
SUBTITLES=~/Desktop/deployingwebapps.vtt
ffmpeg -i $SUBTITLES temp.ass && \
BITRATE=$(ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $INFILE) && \
ffmpeg -i $INFILE -vf ass=temp.ass -vcodec h264_videotoolbox -b:v $BITRATE -c:a copy output_burnedin.mp4
For H.264 with hardware encoding on Windows/Linux with NVIDIA GPU
# Replace with your input video and subtitle file
INFILE=~/Desktop/deployingwebapps.mp4
SUBTITLES=~/Desktop/deployingwebapps.vtt
ffmpeg -i $SUBTITLES temp.ass && \
BITRATE=$(ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $INFILE) && \
ffmpeg -i $INFILE -vf ass=temp.ass -vcodec h264_nvenc -b:v $BITRATE -c:a copy output_burnedin.mp4
For H.265/HEVC with hardware encoding on Mac
# Replace with your input video and subtitle file
INFILE=~/Desktop/deployingwebapps.mp4
SUBTITLES=~/Desktop/deployingwebapps.vtt
ffmpeg -i $SUBTITLES temp.ass && \
BITRATE=$(ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $INFILE) && \
ffmpeg -i $INFILE -vcodec hevc_videotoolbox -tag:v hvc1 -b:v $BITRATE -c:a copy -vf ass=temp.ass output_burnedin.mp4
For H.265/HEVC with hardware encoding on Windows/Linux with NVIDIA GPU
# Replace with your input video and subtitle file
INFILE=~/Desktop/deployingwebapps.mp4
SUBTITLES=~/Desktop/deployingwebapps.vtt
ffmpeg -i $SUBTITLES temp.ass && \
BITRATE=$(ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $INFILE) && \
ffmpeg -i $INFILE -vcodec hevc_nvenc -tag:v hvc1 -b:v $BITRATE -c:a copy -vf ass=temp.ass output_burnedin.mp4
NOTE: HEVC / H.265 hardware encoding can be slow and produce much larger file. For this reason consider using software encoding.
# Replace with your input video and subtitle file
INFILE=~/Desktop/deployingwebapps.mp4
SUBTITLES=~/Desktop/deployingwebapps.vtt
ffmpeg -i $SUBTITLES temp.ass && \
BITRATE=$(ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $INFILE) && \
ffmpeg -i $INFILE -vcodec libx265 -tag:v hvc1 -b:v $BITRATE -c:a copy -vf ass=temp.ass output_burnedin.mp4
If the source video was in H.264 format you can also use the H.265 command to produce a final video in H.265/HEVC format. Note that since H.265 is 25-50% more efficient you should be able to reduce the Bitrate for the same quality video.