Skip to content

Commit

Permalink
Merge pull request #4 from andreia/add_transcode_api
Browse files Browse the repository at this point in the history
Add Transcode API
  • Loading branch information
andreia authored Sep 29, 2024
2 parents 632038d + a49c32d commit 1f42227
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@andreia
66 changes: 66 additions & 0 deletions .github/ISSUE_TEMPLATE/bug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Bug Report
description: Report an Issue or Bug with the Package
title: "[Bug]: "
labels: ["bug"]
body:
- type: markdown
attributes:
value: |
We're sorry to hear you have a problem. Can you help us solve it by providing the following details.
- type: textarea
id: what-happened
attributes:
label: What happened?
description: What did you expect to happen?
placeholder: I cannot currently do X thing because when I do, it breaks X thing.
validations:
required: true
- type: textarea
id: how-to-reproduce
attributes:
label: How to reproduce the bug
description: How did this occur, please add any config values used and provide a set of reliable steps if possible.
placeholder: When I do X I see Y.
validations:
required: true
- type: input
id: package-version
attributes:
label: Package Version
description: What version of our Package are you running? Please be as specific as possible
placeholder: 2.0.0
validations:
required: true
- type: input
id: php-version
attributes:
label: PHP Version
description: What version of PHP are you running? Please be as specific as possible
placeholder: 8.2.0
validations:
required: true
- type: input
id: laravel-version
attributes:
label: Laravel Version
description: What version of Laravel are you running? Please be as specific as possible
placeholder: 9.0.0
validations:
required: true
- type: dropdown
id: operating-systems
attributes:
label: Which operating systems does with happen with?
description: You may select more than one.
multiple: true
options:
- macOS
- Windows
- Linux
- type: textarea
id: notes
attributes:
label: Notes
description: Use this field to provide any other notes that you feel might be relevant to the issue.
validations:
required: false
11 changes: 11 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
blank_issues_enabled: false
contact_links:
- name: Ask a question
url: https://github.com/andreia/php-sdk-dolby-api/discussions/new?category=q-a
about: Ask the community for help
- name: Request a feature
url: https://github.com/andreia/php-sdk-dolby-api/discussions/new?category=ideas
about: Share ideas for new features
- name: Report a security issue
url: https://github.com/andreia/php-sdk-dolby-api/security/policy
about: Learn how to notify us for sensitive bugs
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,39 @@ $analyzeMusicStatusResponse->body(); // "{"path":"/media/analyze/music","status"

#### [Transcode API](https://docs.dolby.io/media-apis/docs/transcode-api-guide)

TODO
[Start Transcoding](https://docs.dolby.io/media-apis/reference/media-transcode-post)

```php
$transcodingResponse = $dolbyApi->api('media')->transcode('inputs-array', 'outputs-array', 'optional-storage-array', 'optional-on-complete-array');
```

E.g.:
```php
$inputs = [
'source' => 'https://dolbyio.s3-us-west-1.amazonaws.com/public/shelby/indoors.original.mp4'
];

$outputs = [
"id" => "my_mp4",
"destination" => "dlb://out/airplane-transcoded.mp4",
"kind" => "mp4",
];

$transcodingResponse = $dolbyApi->api('media')->transcode($inputs, $outputs);
$transcodingResponse->body(); // "{"job_id":"0c1fae6e-39e5-4a36-a076-bf3315d5179f"}"
```

[Get Transcode Results](https://docs.dolby.io/media-apis/reference/media-transcode-get)

```php
$transcodeResultsResponse = $dolbyApi->api('media')->transcodeResults('job-id');
```

E.g.:
```php
$transcodeResultsResponse = $dolbyApi->api('media')->transcodeResults('0c1fae6e-39e5-4a36-a076-bf3315d5179f');
$transcodeResultsResponse->body(); // {"path":"/media/transcode","status":"Success","progress":100,"api_version":"v1.7","result":{}}
```

#### [Music Mastering API](https://docs.dolby.io/media-apis/docs/music-mastering-api-guide)

Expand Down
40 changes: 40 additions & 0 deletions src/Requests/MediaApi/GetTranscodeResults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace DolbyApi\Requests\MediaApi;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class GetTranscodeResults extends Request
{
/**
* HTTP Method
*/
protected Method $method = Method::GET;

public function __construct(
protected string $jobId,
) {}

protected function defaultQuery(): array
{
return [
'job_id' => $this->jobId,
];
}

/**
* Get Transcode Results
*
* For a given job_id, this method will check if the transcoding task
* has completed and return transcoding results.
*
* @see https://docs.dolby.io/media-apis/reference/media-transcode-get
*/
public function resolveEndpoint(): string
{
return '/media/transcode';
}
}
57 changes: 57 additions & 0 deletions src/Requests/MediaApi/StartTranscoding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace DolbyApi\Requests\MediaApi;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class StartTranscoding extends Request implements HasBody
{
use HasJsonBody;

/**
* HTTP Method
*/
protected Method $method = Method::POST;

public function __construct(
protected array $inputs,
protected array $outputs,
protected ?array $storage = null,
protected ?array $onComplete = null,
) {}

protected function defaultBody(): array
{
$body = [
'inputs' => [$this->inputs],
'outputs' => [$this->outputs],
];

if ($this->storage !== null) {
$body['storage'] = $this->storage;
}

if ($this->onComplete !== null) {
$body['on_complete'] = $this->onComplete;
}

return $body;
}

/**
* Start Transcoding
*
* Start transcoding to modify the size, bitrates, and formats for your media.
*
* @see https://docs.dolby.io/media-apis/reference/media-transcode-post
*/
public function resolveEndpoint(): string
{
return '/media/transcode';
}
}
12 changes: 12 additions & 0 deletions src/Resource/MediaApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use DolbyApi\Requests\MediaApi\GetDiagnoseStatus;
use DolbyApi\Requests\MediaApi\GetDownloadUrl;
use DolbyApi\Requests\MediaApi\GetEnhanceStatus;
use DolbyApi\Requests\MediaApi\GetTranscodeResults;
use DolbyApi\Requests\MediaApi\GetUploadUrl;
use DolbyApi\Requests\MediaApi\StartAnalyze;
use DolbyApi\Requests\MediaApi\StartAnalyzeMusic;
use DolbyApi\Requests\MediaApi\StartAnalyzeSpeech;
use DolbyApi\Requests\MediaApi\StartDiagnose;
use DolbyApi\Requests\MediaApi\StartEnhance;
use DolbyApi\Requests\MediaApi\StartTranscoding;
use DolbyApi\Responses\DolbyResponse as Response;

class MediaApiResource extends Resource
Expand Down Expand Up @@ -77,4 +79,14 @@ public function analyzeMusicStatus(string $jobId): Response
{
return $this->connector->send(new GetAnalyzeMusicStatus($jobId));
}

public function transcode(array $inputs, array $outputs, ?array $storage = null, ?array $onComplete = null): Response
{
return $this->connector->send(new StartTranscoding($inputs, $outputs, $storage, $onComplete));
}

public function transcodeResults(string $jobId): Response
{
return $this->connector->send(new GetTranscodeResults($jobId));
}
}
28 changes: 28 additions & 0 deletions tests/Feature/Requests/GetTranscodeResultsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use DolbyApi\DolbyApi;
use DolbyApi\Requests\MediaApi\GetTranscodeResults;
use DolbyApi\Responses\DolbyResponse;
use Saloon\Contracts\Request;
use Saloon\Contracts\Response;

test('can retrieve transcoding results from the api', function () {
$mockClient = mockClient();
$dolbyApi = new DolbyApi('my-api-token');
$dolbyApi->withMockClient($mockClient);

$jobId = 'tr988637-9fb8-4123-62lt-1485r04627st';

$response = $dolbyApi->send(new GetTranscodeResults($jobId));

$mockClient->assertSent(GetTranscodeResults::class);

$mockClient->assertSent(function (Request $request, Response $response) {
return $request instanceof GetTranscodeResults
&& $response->body() == '{"path":"/media/transcode","status":"Success","progress":100,"api_version":"v1.7","result":{}}';
});

expect($response)->toBeInstanceOf(DolbyResponse::class);
});
39 changes: 39 additions & 0 deletions tests/Feature/Requests/StartTranscodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

use DolbyApi\DolbyApi;
use DolbyApi\Requests\MediaApi\StartTranscoding;
use DolbyApi\Responses\DolbyResponse;
use Saloon\Contracts\Request;
use Saloon\Contracts\Response;

/**
* @see https://docs.dolby.io/media-apis/docs/transcoding-media
*/
test('can post to transcode', function () {
$mockClient = mockClient();
$dolbyApi = new DolbyApi('my-api-token');
$dolbyApi->withMockClient($mockClient);

$inputs = [
'source' => 'https://dolbyio.s3-us-west-1.amazonaws.com/public/shelby/indoors.original.mp4',
];

$outputs = [
'id' => 'my_mp4',
'destination' => 'dlb://out/airplane-transcoded.mp4',
'kind' => 'mp4',
];

$response = $dolbyApi->send(new StartTranscoding($inputs, $outputs));

$mockClient->assertSent(StartTranscoding::class);

$mockClient->assertSent(function (Request $request, Response $response) {
return $request instanceof StartTranscoding
&& $response->body() == '{"job_id":"tr988637-9fb8-4123-62lt-1485r04627st"}';
});

expect($response)->toBeInstanceOf(DolbyResponse::class);
});
1 change: 1 addition & 0 deletions tests/Fixtures/Saloon/media/transcode/GET.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Type":"application\/json","Content-Length":"98","Connection":"keep-alive","Server":"CloudFront","Date":"Sun, 29 Sep 2024 17:29:05 GMT","Apigw-Requestid":"{\"0\": \"BNzjHhs-vHcESEA=\",\"1\":\"BNzjEjIFIAMEYnQ=\"}","x-cache":"Miss from cloudfront","Via":"1.1 071a100b70812281a393c7b4228a4288.cloudfront.net (CloudFront)","X-Amz-Cf-Pop":"GRU3-P2","X-Amz-Cf-Id":"LAmYbsQaWmZZcz7NkP5_fSMerBLTScPYQG0SQx6BwvzLTEHdZ_JTQ==","Vary":"Origin"},"data":"{\"path\":\"/media/transcode\",\"status\":\"Success\",\"progress\":100,\"api_version\":\"v1.7\",\"result\":{}}"}
1 change: 1 addition & 0 deletions tests/Fixtures/Saloon/media/transcode/POST.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Type":"application\/json","Content-Length":"49","Connection":"keep-alive","Server":"CloudFront","Date":"Sun, 29 Sep 2024 17:29:05 GMT","Apigw-Requestid":"{\"0\": \"BNzjHhs-vHcESEA=\",\"1\":\"BNzjEjIFIAMEYnQ=\"}","x-cache":"Miss from cloudfront","Via":"1.1 071a100b70811081a393c7b4228a4288.cloudfront.net (CloudFront)","X-Amz-Cf-Pop":"GRU3-P6","X-Amz-Cf-Id":"LAmYbsQaWmZZcz7NkP5_fSDerBLTScPYQG1EWx6BwvzLTEHdZ_JTQ==","Vary":"Origin"},"data":"{\"job_id\":\"tr988637-9fb8-4123-62lt-1485r04627st\"}"}

0 comments on commit 1f42227

Please sign in to comment.