Hi! as I needed to create bulk previews of long videos, like short cuts merged on one scaled video. As i searched for some examples to do this using ffmpeg, stumbled upon a common problem on every snippet there and here... long story short: seeking will make you waste a lot of time unless you seek cleverly.
This script will do the only fast seeking method seeming to work, -ss
from input, pipe a chunk and exit that process. Several chunks are piped the encoder process that writes final file. Theres no temporary files, and if your source is h264, also no transcoding on input reading.
12 seconds of video generated in 14 seconds, almost 12 of them used by encoder scaling
./videosnap.sh video_in.mp4 video_out.mp4
Preconfigure your script with the settings you want (chunk number and duration, output scale, fps...)
- ffmpeg
- ffprobe
- linux (script is portable to windows)
- You can change output settings to use
-c:v copy
and have almost inmediate video split+join as no transcoding will take place - You can change the function that set the split start+duration to your own data
- Its possible to change output to gif, but file will be heavier and worse quality, and you probably will need more filters to make it look good.
- Theres a known problem if you make a mistake on script and execute it, probably pty session will loose stdin
graph LR
A[init] -- duration_to_divisions --> B((ffmpeg chunk))
B --each division-->B
B --> D{ffmpeg out}