Skip to content

Commit 31431dc

Browse files
committed
Join-TsFile.ps1 - processing chunks
- fixed "The command line is too long" by processing files in chunks - updated documentation
1 parent a5df583 commit 31431dc

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

media/Join-TsFile.ps1

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
function Join-TsFile {
22
<#
33
.SYNOPSIS
4-
Helper script for concatenating .ts files
4+
Concatenates .ts video files in a specified location into a single output file
55
66
.DESCRIPTION
7-
This script will join *.ts files from a given directory or list of files passed as an argument
7+
This cmdlet concatenates .ts video files from a specified directory or a list of files into a single output file. The files are processed in chunks to avoid command line length limitations, making the script suitable for handling a large number of files. If a FilesLocation is provided, it sorts the .ts files by creation time. If Files are provided, it uses that list instead. The output file is named "out.ts" by default.
88
99
.PARAMETER FilesLocation
10-
Location of the folder where your DLLs live.
10+
The path to the directory containing the .ts files to concatenate. If not specified, the cmdlet will use the files provided in the -Files parameter.
1111
1212
.PARAMETER Files
13-
Location of the folder where your DLLs live.
13+
An array of System.IO.FileInfo objects representing the .ts files to concatenate. If specified, this parameter overrides FilesLocation.
1414
1515
.PARAMETER OutFile
16-
Location of the folder where your DLLs live.
16+
The name of the output file. By default, this is set to "out.ts".
17+
18+
.PARAMETER ChunkSize
19+
The number of files to process in each chunk. This helps to avoid exceeding the command line length limit when concatenating files.
1720
1821
.EXAMPLE
1922
Join-TsFile "c:\test\"
@@ -22,14 +25,21 @@ Joins all *.ts files found in a "c:\test\" directory
2225
.EXAMPLE
2326
Join-TsFile -Files (gci "c:\test\2")
2427
Joins all files passed as with Files argument
28+
29+
.NOTES
30+
The cmdlet changes the current location to the directory specified by FilesLocation and restores the original location after the operation is complete.
31+
If the total command length is too long for the cmd /c copy operation, reducing the ChunkSize may help avoid errors.
32+
2533
#>
2634
param (
2735
[Parameter(Mandatory = $false, Position = 0)]
2836
[string]$FilesLocation,
2937
[Parameter(Mandatory = $false, Position = 1)]
3038
[System.IO.FileInfo[]]$Files,
3139
[Parameter(Mandatory = $false, Position = 2)]
32-
[string]$OutFile = "out.ts"
40+
[string]$OutFile = "out.ts",
41+
[Parameter(Mandatory = $false, Position = 3)]
42+
[int]$ChunkSize = 1000
3343
)
3444

3545
$location = Get-Location
@@ -42,9 +52,14 @@ Joins all files passed as with Files argument
4252
$tsFile = $Files | Select-Object -First 1
4353
$FilesLocation = $tsFile.Directory
4454
}
45-
$concatenatedNames = $fileNames -join "+"
4655
Set-Location $FilesLocation
47-
cmd /c copy /b $concatenatedNames $OutFile | Out-Null
56+
57+
Copy-Item -Path $fileNames[0] -Destination $OutFile
58+
for ($i = 1; $i -lt $fileNames.Count; $i += $ChunkSize) {
59+
$chunk = $fileNames[$i..([math]::Min($i + $ChunkSize - 1, $fileNames.Count - 1))]
60+
$concatenatedNames = $chunk -join "+"
61+
cmd /c copy /b "$OutFile+$concatenatedNames" $OutFile | Out-Null
62+
}
4863
}
4964
finally {
5065
Set-Location $location

0 commit comments

Comments
 (0)