Skip to content

Commit

Permalink
Implement Stream API Transcribe Video post endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ToshY committed Apr 10, 2024
1 parent 66e6ae5 commit d20e750
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
29 changes: 24 additions & 5 deletions docs/stream-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ $streamApi->fetchVideo(
$streamApi->addCaption(
libraryId: 1,
videoId: 'e7e9b99a-ea2a-434a-b200-f6615e7b6abd',
sourceLanguage: 'jp',
sourceLanguage: 'ja',
body: [
'srclang' => 'jp',
'srclang' => 'ja',
'label' => 'Subtitles (Japanese)',
'captionsFile' => 'MQowMDowMDowMCwwMDAgLS0+IDAwOjAxOjAwLDAwMApOZXZlciBnb25uYSBnaXZlIHlvdSB1cC4K',
],
Expand All @@ -367,7 +367,7 @@ $streamApi->addCaption(

!!! note

- The `sourceLanguage` / `srclang` is the [language shortcode](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) for the caption.
- The `sourceLanguage` / `srclang` is a [two-letter (set 1) language abbreviation](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) for the caption.
- The `captionsFile` requires the file contents to be sent as a base64 encoded string.

#### [Delete Caption](https://docs.bunny.net/reference/video_deletecaption)
Expand All @@ -376,13 +376,32 @@ $streamApi->addCaption(
$streamApi->deleteCaption(
libraryId: 1,
videoId: 'e7e9b99a-ea2a-434a-b200-f6615e7b6abd',
sourceLanguage: 'jp',
sourceLanguage: 'ja',
);
```

!!! note

- The `sourceLanguage` is the [language shortcode](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) for the caption.
- The `sourceLanguage` is a [two-letter (set 1) language abbreviation](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) for the caption.

#### [Transcribe Video](https://docs.bunny.net/reference/video_transcribevideo)

```php
$streamApi->transcribeVideo(
libraryId: 1,
videoId: 'e7e9b99a-ea2a-434a-b200-f6615e7b6abd',
query: [
'language' => 'ja',
'force' => true,
],
);
```

!!! note

- The `language` is a [two-letter (set 1) language abbreviation](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) for transcribing the video.
- The `language` allows multiple values through comma separated values, e.g. `en,ja,ar`.
- Once a video has transcribed you need to set `force` to `true` in order to force a new transcription.

#### [Get OEmbed](https://docs.bunny.net/reference/oembed_getoembed)

Expand Down
40 changes: 40 additions & 0 deletions src/Model/API/Stream/ManageVideos/TranscribeVideo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace ToshY\BunnyNet\Model\API\Stream\ManageVideos;

use ToshY\BunnyNet\Enum\Header;
use ToshY\BunnyNet\Enum\Method;
use ToshY\BunnyNet\Enum\Type;
use ToshY\BunnyNet\Model\AbstractParameter;
use ToshY\BunnyNet\Model\EndpointInterface;
use ToshY\BunnyNet\Model\EndpointQueryInterface;

class TranscribeVideo implements EndpointInterface, EndpointQueryInterface
{
public function getMethod(): Method
{
return Method::POST;
}

public function getPath(): string
{
return 'library/%d/videos/%s/transcribe';
}

public function getHeaders(): array
{
return [
Header::ACCEPT_JSON,
];
}

public function getQuery(): array
{
return [
new AbstractParameter(name: 'language', type: Type::STRING_TYPE, required: true),
new AbstractParameter(name: 'force', type: Type::BOOLEAN_TYPE),
];
}
}
29 changes: 29 additions & 0 deletions src/StreamAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use ToshY\BunnyNet\Model\API\Stream\ManageVideos\RepackageVideo;
use ToshY\BunnyNet\Model\API\Stream\ManageVideos\SetThumbnail;
use ToshY\BunnyNet\Model\API\Stream\ManageVideos\SetThumbnailByBody;
use ToshY\BunnyNet\Model\API\Stream\ManageVideos\TranscribeVideo;
use ToshY\BunnyNet\Model\API\Stream\ManageVideos\UpdateVideo;
use ToshY\BunnyNet\Model\API\Stream\ManageVideos\UploadVideo;
use ToshY\BunnyNet\Model\API\Stream\OEmbed\GetOEmbed;
Expand Down Expand Up @@ -590,6 +591,34 @@ public function deleteCaption(
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\ParameterIsRequiredException
* @param int $libraryId
* @param string $videoId
* @param array<string,mixed> $query
* @return BunnyClientResponseInterface
*/
public function transcribeVideo(
int $libraryId,
string $videoId,
array $query,
): BunnyClientResponseInterface {
$endpoint = new TranscribeVideo();

ParameterValidator::validate($query, $endpoint->getQuery());

return $this->client->request(
endpoint: $endpoint,
parameters: [$libraryId, $videoId],
query: $query,
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
Expand Down

0 comments on commit d20e750

Please sign in to comment.