✂️ trim.lua
mpv script to create a "precise" clip of video files without transcoding.
❓ Differences from the other similar scripts
- trim.lua is aimed only for extraction of clips with no-transcoding. Encodings will never be occured.
- Without encoding, precise video trimming becomes quite tricky and there is a limit what can be done due to keyframe issues.
- Tested several softwares on macOS and as far as I know there is no software that can do it accurately. Well, there is, but none were perfect.
- This script is here to achieve accuracy as much as possible — Making a clip from a file within minimum keyframe distance, without transcoding.
In short,
trim.luaturns mpv into a simple lossless video editor.
📦 Install
curl https://raw.githubusercontent.com/aerobounce/trim.lua/master/trim.lua >> ~/.config/mpv/scripts/trim.lua
⚠️ Dependencies
ffmpegffprobeosascript(to post notification on completion. Only on macOS.)
✂️ Usage
Save Trim Positions (Enables Trim Mode on the first press)
|
|
|---|---|
| h | Save trim start position |
| k | Save trim end position |
| Seeking | |
|---|---|
| shift + h | Seek to trim start position |
| shift + k | Seek to trim end position |
| Adjust Current Keyframe | |
|---|---|
| shift + LEFT | Seek backwards relatively by minimum keyframes |
| shift + RIGHT | Seek forwards relatively by minimum keyframes |
🗜 Concat with ffmpeg
After splitting, you can concat them with a script something like this.
CLIPS=("example_1.mp4" "example_2.mp4")
DESTINATION="concat_example.mp4"
ffmpeg \
-hide_banner \
-loglevel verbose \
-f concat \
-safe 0 \
-auto_convert 0 \
-err_detect ignore_err \
-i <(
while read -r; do
echo "file '$REPLY'"
done <<< "${CLIPS[*]}"
) \
-copy_unknown \
-map_chapters 0 \
-c copy \
"$DESTINATION"Known Issues
- Any embedded media other than video / audio will be lost, such as embedded subtitles.
Todo
- Make
osascriptoptional (as it's macOS only feature) - More accurate keyframe fetching
-
ffmpegandffprobepaths are hard-coded to/usr/local/bin/...