Skip to content

Instantly share code, notes, and snippets.

@dichternebel
Created November 3, 2024 17:43
Show Gist options
  • Save dichternebel/66842ab861bb0af16d5b91568c37d5cb to your computer and use it in GitHub Desktop.
Save dichternebel/66842ab861bb0af16d5b91568c37d5cb to your computer and use it in GitHub Desktop.
Combine or join MTS files splitted by camcorders with file size limit

Combine your splitted MTS files

When recording continuously on older camcorders they may split your video into separate files like the Sony HDR-CX130E does. Now when you just grab your files like 00123.MTS, 00124.MTS etc. and import them into your video editing tool you will notice a small audio gap.

This little script for Windows will put the splitted files back together.

The script assumes that your files are splitted by 1.97GB. If your camera splits by ~ 4GB you have to change the script and set that variable split_size accordingly.

How to

Do not execute this script on the SD card directly!
While this might technically work, you should instead create a dedicated folder and copy your files from the SD Card (e.g. \PRIVATE\AVCHD\BDMV\STREAM) to that folder. Then copy the script into that folder as well and execute it by double-clicking.

That should be it. You will now get your splitted files combined. Please note that there might be more that one combined file if you stopped your recording. That's by design. Joining them would need FFmpeg or something similar and that is not the use case here.

Enjoy!

@echo off
setlocal enabledelayedexpansion
set "split_size=2115508224" :: 1.97 GB in bytes
set "output_count=1"
set "output_base=combined_output_"
set "output=%output_base%01.mts"
set "command=copy /b "
set "last_file_small=false"
set "files_found=false"
for %%F in (*.MTS) do (
set "files_found=true"
set /a "file_size=%%~zF"
if "!last_file_small!"=="true" (
if !file_size! geq %split_size% (
set "execute_command=!command:~0,-1! !output!"
echo Executing: !execute_command!
!execute_command!
if errorlevel 1 (
echo Error occurred during file combination.
goto :error
)
set /a "output_count+=1"
set "output=%output_base%0!output_count!.mts"
if !output_count! geq 10 set "output=%output_base%!output_count!.mts"
set "command=copy /b "
)
)
set "command=!command!%%F+"
if !file_size! lss %split_size% (
set "last_file_small=true"
) else (
set "last_file_small=false"
)
)
if "!files_found!"=="false" (
echo No MTS files were found in the current directory.
echo Nothing was done.
goto :end
)
if not "!command!"=="copy /b " (
set "execute_command=!command:~0,-1! !output!"
echo Executing: !execute_command!
!execute_command!
if errorlevel 1 (
echo Error occurred during final file combination.
goto :error
)
)
echo All MTS files have been combined successfully.
goto :end
:error
echo An error occurred during the process.
exit /b 1
:end
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment