Board logo

标题: [原创代码] Win10 mp4 : 剪輯、取出音樂、設定屬性 [打印本页]

作者: nwm310    时间: 2020-12-15 21:09     标题: Win10 mp4 : 剪輯、取出音樂、設定屬性

把這個ps1 放在 mp4 旁邊
按右鍵 → 用 PowerShell 執行

如果 mp4 不能移動、改名,把 PS 窗口關掉
  1. Add-Type -AssemblyName System.Runtime.WindowsRuntime
  2. [Windows.Storage.StorageFile , Windows.Storage , ContentType=WindowsRuntime] > $null
  3. [Windows.Media.Transcoding.MediaTranscoder, Windows.Media.Transcoding , ContentType=WindowsRuntime] > $null
  4. #learn from https://fleexlab.blogspot.com/2018/02/using-winrts-iasyncoperation-in.html
  5. Function await($WinRtTask, $ResultType = $null) {
  6.     switch($ResultType.count){
  7.         0{ $TaskType = 'IAsyncAction' }
  8.         1{
  9.             if ($ResultType -eq [double]){
  10.                 $TaskType = 'IAsyncActionWithProgress`1'
  11.             }else{
  12.                 $TaskType = 'IAsyncOperation`1'
  13.             }
  14.         }
  15.         2{ $TaskType = 'IAsyncOperationWithProgress`2'}
  16.         default {throw '$ResultType error'}
  17.     }
  18.     $asTask = [System.WindowsRuntimeSystemExtensions].GetMethods() | ?{
  19.         $_.Name -eq 'AsTask' -and
  20.         $TaskType -eq $_.GetParameters().ParameterType.name}
  21.     if ($ResultType.count -gt 0){
  22.         $asTask = $asTask.MakeGenericMethod($ResultType)
  23.     }
  24.     $netTask = $asTask.Invoke($null, @($WinRtTask))
  25.     $netTask.Wait(-1) | Out-Null
  26.     if ($TaskType -like 'IAsyncOperation*'){
  27.         $netTask.Result
  28.     }
  29. }
  30. Function mp4Trim($mp4Path, $startTime = 0, $stopTime = 0, $musicOnly = $false){
  31.    
  32.     $transcoder = [Windows.Media.Transcoding.MediaTranscoder]::new()
  33.     $startTime, $stopTime = $startTime, $stopTime -replace '^\d+$','0:0:$&' -replace '^\d+:\d+$','0:$&'
  34.     $transcoder.TrimStartTime = $startTime
  35.     $transcoder.TrimStopTime = $stopTime
  36.     $a = [Windows.Storage.StorageFile]
  37.     $mp4 = await ($a::GetFileFromPathAsync($mp4Path)) ($a)
  38.    
  39.     $a = [Windows.Media.MediaProperties.MediaEncodingProfile]
  40.     $encProfile = await $a::CreateFromFileAsync($mp4) ($a)
  41.     $mp4SaveName = $mp4.name
  42.     if ($encProfile.video -ne $null -and $musicOnly){
  43.         $q = [Windows.Media.MediaProperties.AudioEncodingQuality]::High
  44.         $a = [Windows.Media.MediaProperties.MediaEncodingProfile]
  45.         $ext = ''
  46.         switch($encProfile.Audio.Subtype){
  47.             'AAC'{$ext = 'm4a'}
  48.             'MP3'{
  49.                 $ext = 'mp3'
  50.                 # if set to $flase, something error when listening to mp3
  51.                 $transcoder.AlwaysReencode = $true
  52.             }
  53.             'WMA9'{$ext = 'wma'}
  54.         }
  55.         if ($ext -ne ''){
  56.             $musicProfile = $a::"Create$ext"($q)
  57.             $musicProfile.Audio = $encProfile.Audio
  58.             $encProfile = $musicProfile
  59.             $mp4SaveName = $mp4.DisplayName + ".$ext"
  60.         }
  61.     }
  62.     if ($encProfile.Audio.Subtype -eq 'WMA8'){$transcoder.AlwaysReencode = $true}
  63.     $a =  [Windows.Storage.StorageFolder]
  64.     $mp4Folder = await ($mp4.GetParentAsync()) ($a)
  65.     $a = [Windows.Storage.StorageFile]
  66.     # GenerateUniqueName=0; ReplaceExisting=1; FailIfExists=2; OpenIfExists=3
  67.     $mp4Save = await ($mp4Folder.CreateFileAsync($mp4SaveName, 0)) ($a)
  68.     $a = [Windows.Media.Transcoding.PrepareTranscodeResult]
  69.     $prepare = await ($transcoder.PrepareFileTranscodeAsync($mp4, $mp4Save, $encProfile)) ($a)
  70.     if($prepare.CanTranscode){
  71.         write-host "transcoding"
  72.         await ($prepare.TranscodeAsync()) ([double])
  73.         Write-Host "transcode complete"
  74.         Write-Host 'wait 2 seconds for transcoder unlock'
  75.         sleep 2
  76.     }else{
  77.         Write-Host "can't transcode"
  78.     }
  79. }
  80. function mp4Property($mp4Path, $title = '', [uint32]$rating = 60, $comment = ''){
  81.     $a = [Windows.Storage.StorageFile]
  82.     $mp4 = await ($a::GetFileFromPathAsync($mp4Path)) ($a)
  83.     $a = [Windows.Storage.FileProperties.VideoProperties]
  84.     $videoP = await ($mp4.Properties.GetVideoPropertiesAsync()) ($a)
  85.     # star   1..12    13..37   38..62   63..87   88..99
  86.     $videoP.Rating = $rating
  87.     await $videoP.SavePropertiesAsync()
  88.     $a = [Windows.Storage.FileProperties.DocumentProperties]
  89.     $docP = await ($mp4.Properties.GetDocumentPropertiesAsync()) ($a)
  90.     $docP.title = $title
  91.     $docP.Comment = $comment
  92.     await $docP.SavePropertiesAsync()
  93.     <#
  94.     $a = [Windows.Storage.FileProperties.ImageProperties]
  95.     $imgP = await ($mp4.Properties.GetImagePropertiesAsync()) ($a)
  96.     $a = [Windows.Storage.FileProperties.MusicProperties]
  97.     $musicP = await ($mp4.Properties.GetMusicPropertiesAsync()) ($a)
  98.     #all properties
  99.     $videoP, $docP, $imgP, $musicP
  100.     #properties (can set)
  101.     $videoP, $docP, $imgP, $musicP | gm 'put_*' -Force
  102.    
  103.     #>
  104.     <#
  105.    
  106.     # https://docs.microsoft.com/en-us/windows/win32/properties/props
  107.    
  108.     $a = [System.Collections.Generic.IDictionary[string, Object]]
  109.     [string[]]$propNames = 'System.Title', 'System.Rating', 'System.Comment'
  110.     await $mp4.Properties.RetrievePropertiesAsync($propNames) ($a) |%{
  111.         "{0} : {1}" -f $_.Key, $_.Value
  112.     }
  113.     $s = 'System.Collections.Generic.KeyValuePair[string,System.Object]'
  114.     $a = [type]$s
  115.     $arr = @(
  116.         $a::new('System.Title', $title),
  117.         $a::new('System.Rating', $rating),
  118.         $a::new('System.Comment', $comment)
  119.     ) -as [type]"$s[]"
  120.     await $mp4.Properties.SavePropertiesAsync($arr)
  121.    
  122.     #>
  123. }
  124. cd -literal $PSScriptRoot
  125. [Environment]::CurrentDirectory = pwd
  126. $files = @(dir *.mp4, *.mp3, *.m4a, *.wmv, *.wma -file)
  127. if ($files.count -ne 0){
  128.     # mp4Trim  filePath  startTime  stopTime  musicOnly
  129.     mp4Trim  $files[0]  0:20  1:50
  130.     # music only
  131.     # mp4Trim  $files[0]  0:20  1:50  $true
  132.    
  133.     # music only & after 0:20
  134.     # mp4Trim  $files[0]  0:20  0     $true
  135.    
  136.     # mp4Property  filePath  title  rating  comment
  137.     # (1 ~ 5 star) rating  1..12    13..37   38..62   63..87   88..99
  138.     mp4Property $files[0] 'a title'  90  'a comment'
  139.    
  140. }
  141. # When mp4 can't move or Rename , close PowerShell console
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2