Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Last active March 19, 2017 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaytaylor/92f24132a92f9dcbc1278fa72a747649 to your computer and use it in GitHub Desktop.
Save jaytaylor/92f24132a92f9dcbc1278fa72a747649 to your computer and use it in GitHub Desktop.
FFMPEG commands for creating a video containing a sequence of images with the original video's audio.

Turn a video into an MP3:

ffmpeg -i filename.mp4 filename.mp3

combine video from first file and audio from second file:

ffmpeg -t 12:54.00 -i mash.mp4 -i dat_beat.mp4 -acodec copy -vcodec copy final-skies.mp4

concat one or more video files:

fffmpeg -f concat -safe 0 -i meta.txt -c copy mash.mp4

where list.txt contains:

file 'file1.mp4'
file 'file2.mp4'
file 'file3.mp4'
file 'file3.mp4'

Also see: http://trac.ffmpeg.org/wiki/Slideshow:

In this example the input images are sequentially named img001.png, img002.png, img003.png, etc.

ffmpeg -framerate 24 -i img%03d.png output.mp4

installing FFMPEG

https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg

#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
set -x
export IFS=$'\n'
mkdir -p out
nextFileIndex=1
for mp3 in songs/*; do
# Convert non-MP3 files to MP3.
if ! [[ "${mp3}" =~ \.[mM][pP]3$ ]]; then
ffmpeg -y -i "${mp3}" -map 0:a audio-input.mp4
songDuration="$(ffmpeg -i audio-input.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , || true)"
mp4="$(basename "$(echo "${mp3}" | sed 's/\.[^\.]*$/.mp4/')")"
if ! [[ "${mp4}" =~ \.mp4$ ]]; then
mp4="${mp4}.mp4"
fi
else
songDuration="$(mp3info -p "%S" "${mp3}")"
# Convert the mp3 to mp4.
ffmpeg -y -i "${mp3}" -map 0:a audio-input.mp4
mp4="$(basename "$(echo "${mp3}" | sed 's/\.[mM][pP]3$/.mp4/')")"
fi
# Select a video file.
i=1
for vidSource in video-sources/*; do
if [[ $i -eq $nextFileIndex ]]; then
break
fi
i=$(($i+1))
done
nextFileIndex=$(($nextFileIndex+1))
outFile="out/${mp4}"
# Skip if destination already exists.
if [[ -f "${outFile}" ]]; then
echo "[info] Skipping song='${mp3}' because outFile='${outFile}' already exists"
continue
fi
echo "MP3=${mp3} VID-SOURCE=${vidSource}"
# Convert gif video inputs to mp4.
if [[ "${vidSource}" =~ \.[gG][iI][fF]$ ]]; then
ffmpeg -y -f gif -i "${vidSource}" video-source.mp4
elif [[ "${vidSource}" =~ \.[wW][eE][bB][mM]$ ]]; then
ffmpeg -y -fflags +genpts -i "${vidSource}" -r 24 video-source.mp4
else
ffmpeg -y -i "${vidSource}" -map 0:v -vcodec copy video-source.mp4
fi
# Build "filenames" list for input video.
i=0
rm -f meta.txt && touch meta.txt
while [[ $i -lt 500 ]]; do
echo "file 'video-source.mp4'" >> meta.txt
i=$(($i+1))
done
# Concat video input to ensure it's long enough.
ffmpeg -y -f concat -safe 0 -i meta.txt -c copy video-input.mp4
# duration="$(ffmpeg -i audio-input.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , )"
# echo $duration
ffmpeg -y -t "${songDuration}" -i video-input.mp4 -i audio-input.mp4 -acodec copy -vcodec copy "${outFile}"
# Check for blank frame.
#ffmpeg -i "${outFile}" -vframes 1 -r 1 -s WxH -f image2 foo-%03d.jpeg
ffmpeg -y -i "${outFile}" -ss 00:00:01 -vframes 1 test.jpg || :
if ! [[ -f test.jpg ]]; then
continue
fi
rm -f audio-input.mp4 video-input.mp4 video-source.mp4 test.jpg
done
rm -f meta.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment