Powershell 对 n 分钟内创建的文件进行分组

问题描述 投票:0回答:1

我的目标是合并行车记录仪创建的视频文件。

驾驶时,相机每n分钟写入一个新文件。如果n=3,30分钟的驾驶会在3分钟内生成10个“创建日期”属性的文件。

文件夹内容示例:

文件名 创建日期 团体
1.ts 2024-09-03 19:00 第 1 组
2.ts 2024-09-03 19:03 第 1 组
3.ts 2024-09-03 19:06 第 1 组
4.ts 2024-09-03 19:20 第2组
5.ts 2024-09-03 19:23 第2组
6.ts 2024-09-03 19:40 第3组
7.ts 2024-09-03 19:44 第 4 组

所需的输出(可能在循环中):

1.ts, 2.ts, 3.ts    
4.ts, 5.ts    
6.ts    
7.ts

如何从Powershell中的输入中获取4组文件名?然后,我会将这些文件组合并为每组一个视频,这样每个驱动器会话就有一个视频。 (传输流文件可以直接连接,无需使用 ffmpeg 等进行处理,因此我可以使用简单的

copy /b "input files" output.ts

谢谢

powershell date grouping
1个回答
0
投票

给你。您可以通过在文本编辑器中复制代码来运行脚本,将文件另存为 yourname.ps1 将其拖放到 powershell 窗口中,并在末尾附加您的参数,然后输入以下命令启动它,例如: myscriptname.ps1“路径文件夹”4 您可以通过右键单击文件夹并“复制地址”将路径复制到该文件夹 timegap 参数默认为 3。我无法测试附加的视频,因为我需要一些示例 ts 文件(笑话)。你最终可以构建它

参数( [字符串]$targetFolder, [int]$时间间隙=3 )

定义主列表以保存所有驱动器集合

带有标题的动作 $listOfLists = @()

# Get all files in the folder, ordered by creation time (oldest first)
$files = Get-ChildItem -Path $targetFolder | Sort-Object CreationTime

# Initialize variables
$previousFile = $null
$driveCollection = @()
$collectionNumber = 1

foreach ($file in $files) {
    if ($previousFile) {
        # Calculate the time difference in minutes between the current and previous file
        $timeDifference = ($file.CreationTime - $previousFile.CreationTime).TotalMinutes

        # If the time difference is more than 3 minutes, start a new drive collection
        if ($timeDifference -gt $timegap) {
            # Add the current drive collection to the list of lists with a title (if not empty)
            if ($driveCollection.Count -gt 0) {
                $title = "Collection $collectionNumber"
                $listOfLists += $title
                $listOfLists += [System.Collections.ArrayList]$driveCollection.Clone()
                $collectionNumber++
            }
            # Start a new drive collection
            $driveCollection.Clear()
        }
    }

    # Add the current file with its creation time to the current drive collection
    $driveCollection += "$($file.Name) - Created: $($file.CreationTime)"

    # Set the current file as the previous file for the next iteration
    $previousFile = $file
}

# Add the last drive collection to the list of lists with a title (if not empty)
if ($driveCollection.Count -gt 0) {
    $title = "Collection $collectionNumber"
    $listOfLists += $title
    $listOfLists += [System.Collections.ArrayList]$driveCollection.Clone()
}

输出列表列表进行验证

$listOfLists

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.