找回密码
 立即注册
搜索
热搜: Java Linux Docker
查看: 24|回复: 0

[Other] 压缩文件视频提取脚本(Windows-PowerShell)

[复制链接]

27

主题

1

回帖

4万

积分

管理员

积分
40179
发表于 2026-5-10 00:23:28 | 显示全部楼层 |阅读模式

压缩文件视频内容提取脚本

主要用来对下载的视频文件为压缩文件格式的内容做提取的脚本;

1 调整Windows的PowerShell安全策略

PowerShell 默认的安全策略会阻止脚本运行。以下是三种由安全到便捷的解决方案。

1.1 仅执行单次脚本(最安全)✅

这种方法不改动系统设置,只临时绕过策略执行该脚本一次。

powershell -ExecutionPolicy Bypass -File "<ps1脚本全路径>"

1.2 为当前用户永久设置(推荐)📌

在安全性和便捷性之间取得最佳平衡,仅对个人账户生效。此方案不需要管理员权限

1.2.1 以普通权限打开 PowerShell(无需右键“以管理员身份运行”)。

1.2.2 执行以下命令:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

策略说明:RemoteSigned 策略意味着你在本地编写的脚本可直接运行,而从网络下载的脚本则需有受信任的签名,这是官方对开发人员推荐的配置。

1.3 使用图形界面更改

适用于不熟悉命令行的用户。

1.3.1 打开Windows设置
1.3.2 搜索并进入开发者选项
1.3.3 在其中找到PowerShell区域,勾选更改执行策略,以允许本地PowerShell脚本在未签名的情况下运行

1.4 额外建议:检查组策略覆盖

如果你在工作电脑上尝试过但无效,可能是因为公司启用了组策略进行覆盖。届时你的修改在 LocalMachine 层级将不生效,建议联系 IT 部门解决。

1.5 补充参考:各策略等级含义(get-executionpolicy)

策略 说明 风险等级
Restricted 禁止运行所有脚本文件(默认) 无风险
AllSigned 仅允许运行收信人签名者签署的脚本 低风险
RemoteSigned 允许本地脚本不限,远程下载脚本需签名 中风险
Unrestrained 允许所有脚本运行,执行来自网络的脚本会有提示 较高风险
Bypass 完全不限制,无任何提示 高风险

2. Shell

新建文件,编辑打开保存。将文件扩展名修改为:.ps1

# 用法示例:
#   .\extract_videos.ps1 -TargetDir "<存储视频压缩文件的全路径>"
# 或简写:
#   .\extract_videos.ps1 "<存储视频压缩文件的全路径>"

param(
    [Parameter(Mandatory=$true, Position=0)]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [string]$TargetDir
)

# ========== 辅助函数:带超时的 Y/N 询问(默认 Y) ==========
function Read-YesNoWithTimeout {
    param(
        [string]$Prompt,
        [int]$TimeoutSeconds = 30,
        [string]$Default = "Y"
    )
    Write-Host "$Prompt (Y/N) - default in $TimeoutSeconds seconds: $Default" -NoNewline
    $startTime = Get-Date
    $inputChar = $null
    while ((Get-Date) - $startTime -lt [TimeSpan]::FromSeconds($TimeoutSeconds)) {
        if ([System.Console]::KeyAvailable) {
            $key = [System.Console]::ReadKey($true)
            if ($key.Key -eq 'Y' -or $key.Key -eq 'y') {
                $inputChar = 'Y'
                break
            } elseif ($key.Key -eq 'N' -or $key.Key -eq 'n') {
                $inputChar = 'N'
                break
            }
            # 忽略其他按键
        }
        Start-Sleep -Milliseconds 200
    }
    if ($null -eq $inputChar) {
        $inputChar = $Default
        Write-Host "`nTimeout, defaulting to $Default"
    } else {
        Write-Host "`n"  # 换行,保持输出美观
    }
    return ($inputChar -eq 'Y' -or $inputChar -eq 'y')
}

# ========== 主脚本 ==========
$zips = Get-ChildItem -Path $TargetDir -Filter "*.zip"
$videoExtensions = @("*.mp4", "*.mkv", "*.avi", "*.mov", "*.wmv", "*.ts", "*.flv")

Write-Host "Found $($zips.Count) zip files in '$TargetDir' to process."

foreach ($zip in $zips) {
    Write-Host "Processing: $($zip.Name)"
    $tempDir = Join-Path $TargetDir ("temp_ext_" + [guid]::NewGuid().ToString().Substring(0,8))

    try {
        Expand-Archive -Path $zip.FullName -DestinationPath $tempDir -Force -ErrorAction Stop

        $videos = Get-ChildItem -Path $tempDir -Include $videoExtensions -Recurse -File

        foreach ($vid in $videos) {
            $destPath = Join-Path $TargetDir $vid.Name

            if (Test-Path $destPath) {
                $newName = "$($vid.BaseName)_$([guid]::NewGuid().ToString().Substring(0,4))$($vid.Extension)"
                $destPath = Join-Path $TargetDir $newName
            }

            Move-Item -Path $vid.FullName -Destination $destPath -Force
            Write-Host "  Moved video: $(Split-Path $destPath -Leaf)"
        }
    } catch {
        Write-Warning "Failed to process $($zip.Name): $_"
    } finally {
        if (Test-Path $tempDir) {
            Remove-Item -Path $tempDir -Recurse -Force
        }
    }
}

Write-Host "Extraction, filtering, and cleanup complete."

# ========== 询问是否删除原始 ZIP(带30秒超时,默认删除) ==========
Write-Host "`nAll zip files have been processed."
$zipCount = $zips.Count
Write-Host "Total zip files found: $zipCount"
$deleteChoice = Read-YesNoWithTimeout -Prompt "Do you want to delete all original .zip files?" -TimeoutSeconds 30 -Default "Y"

if ($deleteChoice) {
    Write-Host "Deleting $zipCount zip files..."
    foreach ($zip in $zips) {
        Remove-Item -Path $zip.FullName -Force
        Write-Host "  Deleted: $($zip.Name)"
    }
    Write-Host "All zip files deleted."
} else {
    Write-Host "Original zip files preserved (no deletion)."
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|saotolls Inc.

GMT+8, 2026-6-20 01:15 , Processed in 0.227588 second(s), 30 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表