2021-11-03

語音轉文字工具 pyTranscriber + 剪映 校正時間軸

一直以來適用 pyTranscriber 來處理影片語音轉文字來上字幕
但是 pyTranscriber 的時間軸實在對不準,常常斷句在奇怪的地方
最近聽說大陸軟體 剪映 也可以自動產生字幕
試用之後發現語音辨識跟 pyTranscriber 這個用 Google 處理的半斤八兩沒有好到哪裡去
但是時間軸卻是較為準確的

所以決定來用他的時間軸與語音辨識搭配 pyTranscriber 的結果來東拼西湊
但是他卻沒有輸出 .srt 字幕檔的功能 Orz
所幸找到他的字幕是存在特定路徑下的 JSON File
於是寫了個簡單的 PowerShell Script 去撈

另外還有個問題是 剪映 產生的字幕是簡體字,所以還需要做繁簡轉換
那我不知道 PowerShell 有沒有函式之類的可以直接拿來用
但是先找了一個 ConvertZZ 來處理
(繁化姬 要用 http GET Query, 字幕數量太大會花很多時間而且可能影響該服務運作所以不採用)

以下是程式


$ConvertZZ = 'C:\Users\Administrator\Downloads\ConvertZZ_v1.0.0.8\ConvertZZ.exe';
#https://github.com/flier268/ConvertZZ/releases

#### ----- Constant ----- ####

$ProjectsFolder = ($env:USERPROFILE + '\AppData\Local\JianyingPro\User Data\Projects\com.lveditor.draft');
$CaptionFile = 'draft_content.json';

#### ----- Program Start ----- ####


if ((Test-Path -Path $ProjectsFolder) -ne $True) {
write-host 'No Project';
exit;
};
$Projects = Get-ChildItem -Directory -Path $ProjectsFolder
if ($Projects.Count -le 0) {
write-host 'No Project';
exit;
};

$CurrentPath = (Get-Location).Path;
if ($CurrentPath -eq '') {
$CurrentPath = (($MyInvocation.MyCommand).Path -Replace ('\\' + ($MyInvocation.MyCommand).Name),'');
};

for ($i=0;$i -lt $Projects.Count;$i++) {
Write-Host ([string]($i+1) + '. ' + $Projects[$i]);
};

write-host
$Index = Read-Host -Prompt "Select Project"

if ($ProjectIndex -match "^[\d\.]+$") {
if ( ($ProjectIndex -ge 0) -and ($ProjectIndex -lt $Projects.Count) ) {
write-host "!!!!"
};
};

write-host

$Index = $Index-1;
if ($Index -match "^[\d\.]+$") {
if ( ($Index -ge 0) -and ($Index -lt $Projects.Count) ) {
$File = (Get-ChildItem -Path $Projects[$Index].FullName | where {$_.Name -eq $CaptionFile}).FullName;
$Json = Get-Content $File -Encoding UTF8 | Out-String | ConvertFrom-Json;

$SubTitle = '';
$SubTitleIndex = 0;
foreach ($Caption in $Json.materials.texts) {

foreach ($Track in $Json.tracks) {
foreach ($Segments in $Track.segments) {
if ($Segments.material_id -eq $Caption.id) {
$SubTitleIndex++;
$SubTitle += ($SubTitleIndex + "`r`n`r`n");
$SubTitle += "`r`n";
$SubTitle += ("{0:hh\:mm\:ss\,fff}" -f [timespan]::fromseconds($Segments.target_timerange.start/1000000));
$SubTitle += " --> ";
$SubTitle += ("{0:hh\:mm\:ss\,fff}" -f [timespan]::fromseconds( ($Segments.target_timerange.start/1000000 + $Segments.target_timerange.duration/1000000) ));
$SubTitle += "`r`n";
$SubTitle += $Caption.content;
$SubTitle += "`r`n`r`n";
};
};
};
};

$TargetPath = (($CurrentPath + '\' + $Projects[$Index].Name + '.srt') -Replace '\\','/');
$SubTitle | Out-File -FilePath ($CurrentPath + '\' + $Projects[$Index].Name + '.srt') -Encoding UTF8;

Start-Process -wait -FilePath $ConvertZZ -ArgumentList ('/i:utf8 /o:utf8 /f:t ' + $TargetPath + ' ' + $TargetPath);

write-host
write-host 'Done'
write-host
write-host $TargetFile
write-host
Start-Process -FilePath 'C:\Windows\System32\notepad.exe' -ArgumentList $TargetPath;
};
};


沒有留言:

張貼留言