dragonzap / subtitle-generator
允许您为任何视频文件生成字幕。支持 WEBVTT 格式
v1.0.2
2024-01-24 13:12 UTC
Requires
- google/cloud-speech: ^1.0
- google/cloud-storage: ^1.0
- laravel/framework: ^9.0|^10.0
README
PHP 库,可以从输入视频文件生成 WebVTT 字幕,需要谷歌云存储桶和谷歌语音识别文本 API,可在 谷歌云控制台 找到。
不使用 Laravel 框架
该库可以使用或不需要 Laravel 框架。请参考 test.php 文件了解不使用 Laravel 框架的使用案例。
运行 composer 命令安装字幕包
composer require dragonzap/subtitle-generator
与 Laravel 框架一起使用
要使用 Laravel 框架,首先安装字幕包,运行 composer 命令安装字幕包
composer require dragonzap/subtitle-generator
接下来打开 config/app.php 文件并更新 'providers' 数组
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
// Add the below provider
Dragonzap\SubtitleGenerator\SubtitleGeneratorProvider::class
//...
];
接下来打开您的 .env 文件并添加以下环境变量
-
GOOGLE_CLOUD_SPEECH_TO_TEXT_PROJECT_ID
:- 描述:这是谷歌云语音识别服务的项目 ID。它在谷歌云平台上唯一标识您的项目。
-
GOOGLE_CLOUD_SPEECH_TO_TEXT_APPLICATION_CREDENTIALS
:- 描述:包含您的服务账户密钥的 JSON 文件的路径。此文件提供应用程序的认证凭据,以便它能够与谷歌云 API 交互。
-
GOOGLE_CLOUD_SPEECH_TO_TEXT_STORAGE_BUCKET
:- 描述:与语音识别处理相关的文件将存储在谷歌云存储桶中的名称。
-
GOOGLE_CLOUD_SPEECH_TO_TEXT_AUDIO_FILE_TMP_DIRECTORY
:- 描述:提取的 WAV 文件将临时存储在此目录中,以供谷歌云语音识别处理。这些文件在作业完成后自动删除。如果未在环境中设置,则默认为
'audio-files'
。
- 描述:提取的 WAV 文件将临时存储在此目录中,以供谷歌云语音识别处理。这些文件在作业完成后自动删除。如果未在环境中设置,则默认为
接下来,您需要在您的 Laravel 目录中运行以下命令以发布配置
php artisan vendor:publish --provider="Dragonzap\SubtitleGenerator\SubtitleGeneratorProvider" --force
现在您应该有一个名为 config/dragonzap_subtitles.php 的新文件,其外观如下
<?php
/*
* Licensed under GPLv2
* Author: Daniel McCarthy
* Dragon Zap Publishing
* Website: https://dragonzap.com
*/
return [
'subtitle_generator' => [
'google' => [
'project_id' => env('GOOGLE_CLOUD_SPEECH_TO_TEXT_PROJECT_ID'),
'credentials' => env('GOOGLE_CLOUD_SPEECH_TO_TEXT_APPLICATION_CREDENTIALS'),
'bucket' => env('GOOGLE_CLOUD_SPEECH_TO_TEXT_STORAGE_BUCKET'),
// This is where the extracted WAV files will be stored for processing by google speech to text
// They will be deleted automatically after the job has been completed
'audio_file_tmp_directory' => env('GOOGLE_CLOUD_SPEECH_TO_TEXT_AUDIO_FILE_TMP_DIRECTORY', 'audio-files'),
]
],
];
您可以重新配置配置文件以使用您选择的任何环境变量
接下来,您可以创建测试命令以测试此功能
php artisan make:command TestCommand
将新命令文件替换为以下内容
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Dragonzap\SubtitleGenerator\SubtitleGeneratingService;
class TestCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
protected $list_id;
protected SubtitleGeneratingService $service;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(SubtitleGeneratingService $service)
{
parent::__construct();
$this->service = $service;
}
public function handle()
{
$operation_id = $this->service->beginGeneratingSubtitles(public_path('output.mp4'));
while(1)
{
$result = $this->service->checkSubtitleGenerationOperation($operation_id);
if ($result['status'] == 'success')
{
$this->info('Success');
$this->info($result['subtitles']);
break;
}
else
{
$this->info('In Progress');
}
sleep(5);
}
}
}
在此示例中,将为 output.mp4 文件生成字幕。您可以将 $operation_id 存储在数据库中,以频繁轮询而不是阻塞等待字幕生成。大文件可能需要几分钟才能生成字幕,因此请考虑将 $operation_id 存储在数据库中,以频繁轮询,直到达到成功状态。