Merge and encode single videos split into multiple mp4 files with PowerShell using MKVToolNix and Handbrake
Merge and encode single videos split into multiple mp4 files in the given GoPro export folder and removes original files if successful. various unique recording in the same directory.
Requires MKVmerge and HandBrakeCLI installed in C:\Program Files\MKVToolNix\ and C:\Program Files\HandBrake\ respectively.
Custom paths can be setup using the parameters -MKVMergeLocation
and -HandbrakeLocation
.
See official Handbrake presets
Output file is Encoded-{original filename}-{date of subfolder}
-
-dir
Defaults to the current directory -
-preset
The default preset is "Vimeo YouTube HQ 1440p60 2.5K". -
-MKVMergeLocation
Location of mkvmerge.exe, defaults to 'C:\Program Files\MKVToolNix', -
-HandbrakeLocation
Location of HandBrakeCLI.exe, defaults to'C:\Program Files\HandBrake', -
-MKVMergeExtraParam
Extra parameters for MKVMerge -
-HandbrakeExtraParam
Extra parameters for Handbrake -
-test
Test merge and encode three seconds of each video, without deleting the original files -
-delete
Delete original files if no error is found. Defaults to true -
-mergeAll
Ignore file naming and merge all files in the directory -
-noEncoding
Skip encoding step, only merge
.\goproMergeEncode.ps1 -dir "d:\Video\GoPro\2019-04-21\HERO5 Black 1\"
If you have a backlog of videos to split, you can create a chain of PowerShell commands
.\goproMergeEncode.ps1 -dir "D:\Video\GoPro\2018-10-19\HERO5 Black\"; .\goproMergeEncode.ps1 -dir "D:\Video\GoPro\2018-12-04\HERO5 Black 1\"; .\goproMergeEncode.ps1 -dir "D:\Video\GoPro\2018-12-11\HERO5 Black 1\";
powershell.exe –ExecutionPolicy Bypass -noexit "& 'D:\goproMergeEncode.ps1' -dir 'D:\Video\GoPro\2019-04-19\HERO5 Black 1\'"
Output file for D:\Video\GoPro\2019-04-21\HERO5 Black 1\GOPR2548.MP4 is Encoded-GOPR2548-20190421.mp4
By default, Windows doesn't allow to run PowerShell scripts, see execution policies
Open Start, search for PowerShell, right-click the top-result and click the Run as administrator option.
Check your execution policy by running the command Get-ExecutionPolicy
To change to a policy that allows you to run scripts, run PowerShell as administrator and run Set-ExecutionPolicy RemoteSigned
To unblock this file only, go to the folder you downloaded the file and run Unblock-File .\goproMergeEncode.ps1
Run from command line powershell –ExecutionPolicy Bypass
All scripts run during this session will bypass the restrictions.
Based on the file name format GOPR2548.MP4 and multiple files GP012548.MP4, GP022548.MP4,... and folder structure ..\GoPro\2019-04-21\HERO5 Black 1
Note that the script expects an arrangement that works for single videos for the camera models: HD HERO2, HERO3, HERO3+, HERO (2014), HERO Session, HERO4, HERO5 Black, HERO5 Session, HERO (2018)
For HERO6 Black, & HERO7 (White, Silver, Black) or other formats use -mergeAll parameter. See GoPro Camera File Naming Convention
Note that HandBrake doesn't support merging.
- If you don't like PowerShell, although it's OSS and available in all platforms now, for linux bash you can use https://github.com/KonradIT/gopro-linux which also have much more features.
- For tools with a user interface use https://www.mp4joiner.org/en/, http://www.dashware.net/ or other tools from https://www.fosshub.com/Video-Editors.html
This script was made quickly with the goal for me to learn more about PowerShell. Probably would be better to use ffmpeg, but I like the presets of HandBrake, and my focus was mostly on learning to script something using PowerShell.
Decided to make it open source, since others might find useful, but now I have spent more time on documentation than actual scripting :)
All code reviews, suggestions, recommendations, pieces of advice, optimizations, insults are welcomed. contact@alexandrosk0.dev