saotolls 发表于 2026-5-10 00:23:28

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

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

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

## 1 调整Windows的PowerShell安全策略

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

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

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

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

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

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

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

1.2.2 执行以下命令:

```powrershell
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`

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

param(
   
   
    $TargetDir
)

# ========== 辅助函数:带超时的 Y/N 询问(默认 Y) ==========
function Read-YesNoWithTimeout {
    param(
      $Prompt,
      $TimeoutSeconds = 30,
      $Default = "Y"
    )
    Write-Host "$Prompt (Y/N) - default in $TimeoutSeconds seconds: $Default" -NoNewline
    $startTime = Get-Date
    $inputChar = $null
    while ((Get-Date) - $startTime -lt ::FromSeconds($TimeoutSeconds)) {
      if (::KeyAvailable) {
            $key = ::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_" + ::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)_$(::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)."
}
```
页: [1]
查看完整版本: 压缩文件视频提取脚本(Windows-PowerShell)