今まで英語CC字幕で英語勉強していて、英語アニメにCC字幕がついているか 血眼になって探していたりしたのですが、今の時代、なければ作るができるらしく、
Whisperを用いてCC字幕を当てることができるらしいという情報を発見(5chの英語板) したので試してみました。 かなり実用的でCPUでも動くということ感動しました。
ただ、ライブラリが出力するSRTファイルを元のMP4にmuxするという一連の動作を さらにバッチ処理するところまでできないと 実用上意味がないなと思ったのですが、
そうだ、Claudeに聞けばいいんだってエウレカ体験しまして、
Claude3(Opus)に尋ねて、 Whisperの処理をするpyファイルがいままではコードに直書きだったのですが、 「バッチ処理に使いたいから動画ファイルの引数を受け取ってそれに対して処理するようにして」 などの質問を理解してくれて まずこんなpyファイルになりました
import sys import stable_whisper # Check if the MP4 file name is provided as an argument if len(sys.argv) < 2: print("Please provide the MP4 file name as an argument.") sys.exit(1) # Get the MP4 file name from the command-line argument mp4_file = sys.argv[1] # Load the Whisper model model = stable_whisper.load_model('large-v2', device='cpu') # Transcribe the MP4 file results = model.transcribe(mp4_file, language='en') # Specify the fixed SRT file name srt_file = "temp.srt" # Convert the transcription results to SRT format stable_whisper.result_to_srt_vtt(results, srt_file, word_level=False)
このファイルを回すpowershellファイル、が以下のようになりました。
# Specify the name of the test.py script $testScript = "test.py" # Get all MP4 files in the current directory $inputMP4Files = Get-ChildItem -Filter "*.mp4" # Process each MP4 file foreach ($inputMP4 in $inputMP4Files) { # Get the base name of the input MP4 file (without extension) $baseName = $inputMP4.BaseName # Specify the name of the exported SRT file $exportedSRT = "temp.srt" # Specify the name of the output MP4 file with the muxed SRT $outputMP4 = "$baseName`_muxed.mp4" # Run the test.py script with the input MP4 file python $testScript $inputMP4 # Check if the exported SRT file exists if (Test-Path $exportedSRT) { # Mux the input MP4 file with the exported SRT file using FFmpeg ffmpeg -i $inputMP4 -i $exportedSRT -c copy -c:s mov_text $outputMP4 Write-Host "Muxing completed for $inputMP4. Output file: $outputMP4" # Delete the temporary SRT file Remove-Item $exportedSRT } else { Write-Host "Exported SRT file not found for $inputMP4." } }
やってみたら動いて、ほんとにこんなじだいになってしまったんだなぁと感嘆しました。 もちろん、新しいライブラリということで最初の使い方はAIも知らないので 入れてあげないといけないのですがそれでもすごいなと思います。
おまけに、powershellのスクリプト実行制限まで言及してくれて、よく読んでなくて動かなかったんですけど、 そこ直したら動くというすごい”介護プログラミング”ができて面白かったです。