【Mac】Vimeoの日本語自動生成を修正利用する。


私の場合、Vimeoの一番安いクラスPlusの会員、700円/月、250GBになっています。現在はもうPlusの募集は無く、新体系でStarterが登場し1,200円/月、2TBということになっています。Vimeoの有料会員のサービスに、日本語字幕の自動生成が含まれるようになりました。これを使うのが経済的です。ただし、まだ走りの感で、自動生成される文字起こしは、かなり雑です。そこで、修正作業を効率化するフロー及びツールを作りましたので、ご紹介します。

ポイント解説

1 Vimeoの日本語(自動生成)を利用しダウンロードします。ところが。

ところが、自動生成したものは、下のように、文字と文字の間に、不規則に、空白が入っており、このまま使いたくありません

Vimeoで最初に自動生成されるvttファイル( auto_generated_captions.vtt )は、上のようにかなり低品質。これを活用するか、別の文字起こしアプリを使うか迷うところだが。。

2 MacのAutomatorを使い、Appleスクリプトを記述します。

(1)文字と文字の空白を削除する。

(2)”–>”の前後の空白も削除されてしまうので、” –> “に置き換える。

(3)先頭行を”WEBVTT “だけにして、2行目に空白行を挿入する。

MacのAutomator起動→クイックアクションを選択→AppleScriptを実行を選択→ワークフローが受け取る現在の項目:ファイルまたはフォルダ→スクリプトをコピペ→名前を付けて保存

 Vimeo修正用 Appleスクリプト:コピーしてご利用ください。

(1)「 AppleScriptを実行」を挿入

on run {input, parameters}
	set filePath to POSIX path of input
	do shell script "sed -i '' -e 's/ //g' " & quoted form of filePath
	return input
end run

(2)「 AppleScriptを実行」を挿入

on run {input, parameters}
	set filePath to POSIX path of input
	do shell script "sed -i '' -e 's/-->/ --> /g' " & quoted form of filePath
	return input
end run

(3)「 AppleScriptを実行」を挿入

on run {input, parameters}
    set filePath to POSIX path of input
    do shell script "sed -i '' -e '1s/.*/WEBVTT /; 2i\\ ' " & quoted form of filePath
    return input
end run

3 クイックアクションで、vttファイルを修正します。

コピーしたvttファイルの上で右クリック→クイックアクション→vimeoのvttの修正、を起動します。

すると、以下のように、修正されます。

これを元に、テキストエディットで更に手直しを行い、vttファイルを完成します。それをvimeoの字幕へアップロードします。

上記説明とは直接には関係ありませんが、この際、あると便利なものを作っておきます。

vttからsrt生成、srtからvtt生成についても、あちこちで変換ツールが提供されていますが、Macとして一番使いやすいのは、Finderから対象ファイルの上で右クリックで操作できるのが一番ラクです。そのためのAppleスクリプトは以下です。ご利用ください。

vttからsrt生成用Appleスクリプト

(1)「 AppleScriptを実行」を挿入

-- 手順1 内容を複製した新たなsrtファイルを作成する。
on run {input, parameters}
	set vttFilePath to POSIX path of input
	
	-- 拡張子をSRTに置き換える
	set newFilePath to replaceExtension(vttFilePath, "srt")
	
	-- ファイル名を作成する
	set fileName to do shell script "basename " & quoted form of vttFilePath
	set baseName to text 1 thru ((offset of "." in fileName) - 1) of fileName
	set newFileName to baseName & ".srt"
	
	-- 新しいファイルのパスを生成する
	set newFilePath to (text 1 thru -((count fileName) + 1) of vttFilePath) & newFileName
	
	-- ファイルをコピーして新しいファイルを作成する
	do shell script "cp " & quoted form of vttFilePath & " " & quoted form of newFilePath
	
	-- 手順2に新しいファイルのパスを渡す
	return {newFilePath}
end run

-- 拡張子を置き換える補助関数
on replaceExtension(filePath, newExtension)
	set AppleScript's text item delimiters to "."
	set pathItems to text items of filePath
	set lastItem to last item of pathItems
	set last item of pathItems to newExtension
	set AppleScript's text item delimiters to "."
	set newPath to pathItems as text
	return newPath
end replaceExtension

(2)「 AppleScriptを実行」を挿入

-- 手順2   次に、手順1で新たに作ったSRTファイルに対して、
-- (1) 文字列”WEBVTT”のある行は削除する。
-- (2) "-->" がある行は、行の中の"."を","に置き換える。
-- を最後まで繰り返し、更新する。
on run {input, parameters}
	set newFilePath to POSIX path of (first item of input)
	
	-- ファイル内容を読み込む
	set fileContent to read newFilePath as «class utf8»
	set fileLines to paragraphs of fileContent
	
	set newContent to ""
	
	-- 条件に基づいてファイル内容を更新
	repeat with i from 1 to count fileLines
		set lineText to item i of fileLines
		
		-- (1) 文字列”WEBVTT”のある行は削除する。
		if lineText does not contain "WEBVTT" then
			-- (2) "-->" がある行は、行の中の"."を","に置き換える。
			if lineText contains "-->" then
				set newContent to newContent & textReplace(lineText, ".", ",") & linefeed
			else
				set newContent to newContent & lineText & linefeed
			end if
		end if
	end repeat
	
	-- ファイルに新しい内容を書き込む
	set fileHandle to open for access newFilePath with write permission
	set eof of fileHandle to 0
	write newContent to fileHandle as «class utf8»
	close access fileHandle
	
	return input
end run

-- 文字列中の特定の文字を置き換える
on textReplace(inputText, findText, replaceText)
	set AppleScript's text item delimiters to findText
	set textItems to text items of inputText
	set AppleScript's text item delimiters to replaceText
	set newText to textItems as text
	set AppleScript's text item delimiters to ""
	return newText
end textReplace

srtからvtt生成用Appleスクリプト

(1)「 AppleScriptを実行」を挿入

-- 手順1 内容を複製した新たなvttファイルを作成する。
on run {input, parameters}
	set srtFilePath to POSIX path of input
	
	-- 拡張子をVTTに置き換える
	set newFilePath to replaceExtension(srtFilePath, "vtt")
	
	-- ファイル名を作成する
	set fileName to do shell script "basename " & quoted form of srtFilePath
	set baseName to text 1 thru ((offset of "." in fileName) - 1) of fileName
	set newFileName to baseName & ".vtt"
	
	-- 新しいファイルのパスを生成する
	set newFilePath to (text 1 thru -((count fileName) + 1) of srtFilePath) & newFileName
	
	-- ファイルをコピーして新しいファイルを作成する
	do shell script "cp " & quoted form of srtFilePath & " " & quoted form of newFilePath
	
	-- 手順2に新しいファイルのパスを渡す
	return {newFilePath}
end run

-- 拡張子を置き換える補助関数
on replaceExtension(filePath, newExtension)
	set AppleScript's text item delimiters to "."
	set pathItems to text items of filePath
	set lastItem to last item of pathItems
	set last item of pathItems to newExtension
	set AppleScript's text item delimiters to "."
	set newPath to pathItems as text
	return newPath
end replaceExtension

(2)「 AppleScriptを実行」を挿入

-- 手順2   次に、手順1で新たに作ったVTTファイルに対して、
-- (1) 先頭行に文字列"WEBVTT"を含む行を挿入する。
-- (2) 2行目に空白の行を挿入する。
-- (3) 3行目以降から、読み込んだデータを書き込む。
-- (4) "-->" がある行は、行の中の","を"."に置き換える。

on run {input, parameters}
	set newFilePath to POSIX path of (first item of input)
	
	-- ファイル内容を読み込む
	set fileContent to read newFilePath as «class utf8»
	set fileLines to paragraphs of fileContent
	
	set newContent to "WEBVTT" & linefeed & linefeed
	
	-- 条件に基づいてファイル内容を更新
	repeat with i from 1 to count fileLines
		set lineText to item i of fileLines
		
		if i > 1 then -- 読み込みデータの2行目以降
			if lineText contains "-->" then -- (4)
				set lineText to textReplace(lineText, ",", ".")
			end if
		end if
		
		set newContent to newContent & lineText & linefeed
	end repeat
	
	-- ファイルに新しい内容を書き込む
	set fileHandle to open for access newFilePath with write permission
	set eof of fileHandle to 0
	write newContent to fileHandle as «class utf8»
	close access fileHandle
	
	return input
end run

-- 文字列中の特定の文字を置き換える
on textReplace(inputText, findText, replaceText)
	set AppleScript's text item delimiters to findText
	set textItems to text items of inputText
	set AppleScript's text item delimiters to replaceText
	set newText to textItems as text
	set AppleScript's text item delimiters to ""
	return newText
end textReplace

4 まとめ

字幕制作の面倒さですが、それは、動画は、頻繁に追加、修正、削除が行われますので、それに伴い音声の発声タイミングも変更されなければなりません。そこを自動化して欲しいです。つまり、ビデオ編集アプリに、字幕起こし機能が実装されるのが一番便利であると思います。DaVinci Resolveはすでにそうなっていますので、Final Cut Proにも、早く実装されるのを期待します。

Vimeoの自動生成を使うのは、動画の尺は変えない段階まで来てから、字幕だけ付けるという場合、有効でしょう。あるいはもしFinal Cut Proで動画編集をしているなら、Final Cut Proの字幕ファイル読み込みはsrtファイルだけなので、上に紹介したAppleスクリプトでvtt→srtを生成して、srtを読み込む方法もあります。

字幕起こしアプリVrewも、代替方法とします。リアルタイムの会話補助用は「音声認識字幕ちゃん」が優れています。

器材記録:

Vimeo(PLUS会員)
Automator
テキストエディット
Mac mini M2 Ventura
BGM:オリジナル音楽

以上です。

↑広告-Google AdSense-