[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创代码] PowerShell实现Win10 影片截圖

本帖最后由 nwm310 于 2019-11-23 19:41 编辑

11.23修正:檔名 & 的問題
===

把ps1 放到影片旁邊
對著ps1按右鍵 → 用 PowerShell 執行

Win10 適用

getMp4Pic 用法
#預設值:從 0:0:0 開始截圖,截圖到結束,間隔 0:1:0  一分鐘
getMp4Pic 'C:\3.mp4'  -start 0 -end -1 -step 0:1:0 -w 0 -h 0

#效果同上。可省略其他參數。
getMp4Pic  'C:\3.mp4'


#只截一張圖 0:0:5
getMp4Pic 'C:\3.mp4'  0:0:5   0:0:5

#從 0:0:5 開始截圖,截圖到結束,間隔 0:0:30 三十秒
getMp4Pic 'C:\3.mp4'   0:0:5   -step  0:0:30

#從 0:0:5 開始截圖,截圖到0:3:0,間隔 0:1:0 一分鐘
getMp4Pic 'C:\3.mp4'  0:0:5   0:3:0

#從 0:0:5 開始截圖,截圖到0:3:0,間隔 0:0:20 二十秒
getMp4Pic 'C:\3.mp4'  0:0:5   0:3:0  0:0:20

#設定長度、寬度
getMp4Pic 'C:\3.mp4'  0:0:5   0:3:0  0:0:20 -w 640 -h 480


第33行、第39行:前面不能有空格
  1. function getMp4Pic{
  2.     param(
  3.         [io.fileinfo]$mp4,
  4.         [timespan]$startTime = '0:0:0' ,
  5.         [timespan]$endTime = '-1' ,
  6.         [timespan]$step = '0:1:0' ,
  7.         [uint32]$width = 0 ,
  8.         [uint32]$height = 0
  9.     )
  10.     #learn from https://fleexlab.blogspot.com/2018/02/using-winrts-iasyncoperation-in.html
  11.     Add-Type -AssemblyName System.Runtime.WindowsRuntime
  12.     $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() |
  13.         ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and
  14.         $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' }   )[0]
  15.     Function Await($WinRtTask, $ResultType) {
  16.     $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
  17.     $netTask = $asTask.Invoke($null, @($WinRtTask))
  18.     $netTask.Wait(-1) | Out-Null
  19.     $netTask.Result
  20.     }
  21.     #http://blog.bagusandryan.com/get-video-thumbnail-storagefile-uwp-732/
  22.     #https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/imaging
  23.     [Windows.Storage.StorageFile , Windows.Storage , ContentType=WindowsRuntime] > $null
  24.     [Windows.Media.Editing.MediaComposition , Windows.Media.Editing , ContentType=WindowsRuntime] > $null
  25.     [Windows.Graphics.Imaging.ImageStream , Windows.Graphics.Imaging , ContentType=WindowsRuntime] > $null
  26.     $xmlPath = Join-Path  $mp4.DirectoryName  "MediaClip11111xml.xml"
  27.     $mp4PathInXml = "$mp4"  -replace  '&','&'
  28. @"
  29. <?xml version="1.0" encoding="UTF-8"?>
  30. <Composition>
  31. <Clips>
  32.     <MediaClip Path="$mp4PathInXml" />
  33. </Clips>
  34. </Composition>
  35. "@ | sc -Literal  $xmlPath  -Enc UTF8
  36. # "@  must at begin of line
  37.     $a = [Windows.Storage.StorageFile]
  38.     $xmlFile = await ($a::GetFileFromPathAsync($xmlPath)) ($a)
  39.     #===== MediaComposition =====
  40.     $a = [Windows.Media.Editing.MediaComposition]
  41.     $m01 = await ($a::LoadAsync($xmlFile)) ($a)
  42.     $NearestKeyFrame = 1
  43.     if ($endTime -eq '-1'){ $endTime = $m01.Clips.OriginalDuration }
  44.     $outFolder = Join-Path $mp4.DirectoryName  ($mp4.BaseName + "_$startTime" -replace ':','_')
  45.     md $outFolder 2>$null >$null
  46.     for ($time = $startTime; $time -le $endTime; $time += $step){
  47.         $a = [Windows.Graphics.Imaging.ImageStream]
  48.         $mp4Thumb = await ($m01.GetThumbnailAsync($time, $width, $height, $NearestKeyFrame)) ($a)
  49.         $reader = [Windows.Storage.Streams.DataReader]::new($mp4Thumb.GetInputStreamAt(0))
  50.         $reader.LoadAsync($mp4Thumb.Size) > $null
  51.         $bytes = new-object byte[] ($mp4Thumb.Size)
  52.         $reader.ReadBytes($bytes)
  53.         
  54.         #out
  55.         $outName = $mp4.BaseName + ("_$time" -replace ':','_') + '.jpg'
  56.         $outPath = Join-Path $outFolder  $outName
  57.         [System.IO.File]::WriteAllBytes($outPath, $bytes)
  58.     }
  59.     del $xmlPath
  60. }
  61. #=======
  62. cd -literal $PSScriptRoot
  63. [Environment]::CurrentDirectory = pwd
  64. $files = @(dir *.mp4,*.mkv -file)
  65. if ($files.count -ne 0){
  66.     getMp4Pic  $files[0]  -start 0:0:5  -end 0:0:5  # -step 0:0:20 -w 640 -h 480
  67. }
复制代码

返回列表