Skip to content

Commit

Permalink
refactor: dubbing response can be jsonified & renamings that more in …
Browse files Browse the repository at this point in the history
…line with the API reference
  • Loading branch information
austinbhale committed Jul 6, 2024
1 parent 958a27d commit 348188c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
28 changes: 14 additions & 14 deletions ElevenLabs-DotNet-Tests/Test_Fixture_07_DubbingEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ public async Task Test_01_Dubbing_File()
Watermark = false,
};

(string dubbingId, float expectedDurationSecs) = await ElevenLabsClient.DubbingEndpoint.StartDubbingAsync(request);
Assert.IsFalse(string.IsNullOrEmpty(dubbingId));
Assert.IsTrue(expectedDurationSecs > 0);
Console.WriteLine($"Expected Duration: {expectedDurationSecs:0.00} seconds");
DubbingResponse response = await ElevenLabsClient.DubbingEndpoint.StartDubbingAsync(request);
Assert.IsFalse(string.IsNullOrEmpty(response.DubbingId));
Assert.IsTrue(response.ExpectedDurationSeconds > 0);
Console.WriteLine($"Expected Duration: {response.ExpectedDurationSeconds:0.00} seconds");

Assert.IsTrue(await ElevenLabsClient.DubbingEndpoint.WaitForDubbingCompletionAsync(dubbingId, progress: new Progress<string>(msg => Console.WriteLine(msg))));
Assert.IsTrue(await ElevenLabsClient.DubbingEndpoint.WaitForDubbingCompletionAsync(response.DubbingId, progress: new Progress<string>(msg => Console.WriteLine(msg))));

FileInfo srcFile = new(audio.FilePath);
FileInfo dubbedPath = new($"{srcFile.FullName}.dubbed.{request.TargetLanguage}{srcFile.Extension}");
{
await using FileStream fs = File.Open(dubbedPath.FullName, FileMode.Create);
await foreach (byte[] chunk in ElevenLabsClient.DubbingEndpoint.GetDubbedFileAsync(dubbingId, request.TargetLanguage))
await foreach (byte[] chunk in ElevenLabsClient.DubbingEndpoint.GetDubbedFileAsync(response.DubbingId, request.TargetLanguage))
{
await fs.WriteAsync(chunk);
}
Expand All @@ -45,7 +45,7 @@ public async Task Test_01_Dubbing_File()

FileInfo transcriptPath = new($"{srcFile.FullName}.dubbed.{request.TargetLanguage}.srt");
{
string transcriptFile = await ElevenLabsClient.DubbingEndpoint.GetTranscriptForDubAsync(dubbingId, request.TargetLanguage, "srt");
string transcriptFile = await ElevenLabsClient.DubbingEndpoint.GetTranscriptForDubAsync(response.DubbingId, request.TargetLanguage, "srt");
await File.WriteAllTextAsync(transcriptPath.FullName, transcriptFile);
}
Assert.IsTrue(transcriptPath.Exists);
Expand All @@ -67,18 +67,18 @@ public async Task Test_02_Dubbing_Url()
Watermark = true,
};

(string dubbingId, float expectedDurationSecs) = await ElevenLabsClient.DubbingEndpoint.StartDubbingAsync(request);
Assert.IsFalse(string.IsNullOrEmpty(dubbingId));
Assert.IsTrue(expectedDurationSecs > 0);
Console.WriteLine($"Expected Duration: {expectedDurationSecs:0.00} seconds");
DubbingResponse response = await ElevenLabsClient.DubbingEndpoint.StartDubbingAsync(request);
Assert.IsFalse(string.IsNullOrEmpty(response.DubbingId));
Assert.IsTrue(response.ExpectedDurationSeconds > 0);
Console.WriteLine($"Expected Duration: {response.ExpectedDurationSeconds:0.00} seconds");

Assert.IsTrue(await ElevenLabsClient.DubbingEndpoint.WaitForDubbingCompletionAsync(dubbingId, progress: new Progress<string>(msg => Console.WriteLine(msg))));
Assert.IsTrue(await ElevenLabsClient.DubbingEndpoint.WaitForDubbingCompletionAsync(response.DubbingId, progress: new Progress<string>(msg => Console.WriteLine(msg))));

string assetsDir = Path.GetFullPath("../../../Assets");
FileInfo dubbedPath = new(Path.Combine(assetsDir, $"online.dubbed.{request.TargetLanguage}.mp4"));
{
await using FileStream fs = File.Open(dubbedPath.FullName, FileMode.Create);
await foreach (byte[] chunk in ElevenLabsClient.DubbingEndpoint.GetDubbedFileAsync(dubbingId, request.TargetLanguage))
await foreach (byte[] chunk in ElevenLabsClient.DubbingEndpoint.GetDubbedFileAsync(response.DubbingId, request.TargetLanguage))
{
await fs.WriteAsync(chunk);
}
Expand All @@ -88,7 +88,7 @@ public async Task Test_02_Dubbing_Url()

FileInfo transcriptPath = new(Path.Combine(assetsDir, $"online.dubbed.{request.TargetLanguage}.srt"));
{
string transcriptFile = await ElevenLabsClient.DubbingEndpoint.GetTranscriptForDubAsync(dubbingId, request.TargetLanguage, "srt");
string transcriptFile = await ElevenLabsClient.DubbingEndpoint.GetTranscriptForDubAsync(response.DubbingId, request.TargetLanguage, "srt");
await File.WriteAllTextAsync(transcriptPath.FullName, transcriptFile);
}
Assert.IsTrue(transcriptPath.Exists);
Expand Down
18 changes: 8 additions & 10 deletions ElevenLabs-DotNet/Dubbing/DubbingEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public sealed class DubbingEndpoint(ElevenLabsClient client) : ElevenLabsBaseEnd
/// in seconds if the operation succeeds.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="request"/> is <see langword="null"/>.</exception>
public async Task<(string DubbingId, float ExpectedDurationSecs)> StartDubbingAsync(DubbingRequest request, CancellationToken cancellationToken = default)
public async Task<DubbingResponse> StartDubbingAsync(DubbingRequest request, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(request);

using MultipartFormDataContent content = new();
using MultipartFormDataContent content = [];

if (!string.IsNullOrEmpty(request.Mode))
{
Expand Down Expand Up @@ -126,10 +126,8 @@ public sealed class DubbingEndpoint(ElevenLabsClient client) : ElevenLabsBaseEnd
using HttpResponseMessage response = await client.Client.PostAsync(GetUrl(), content, cancellationToken).ConfigureAwait(false);
await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false);

using var responseDoc = JsonDocument.Parse(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
string dubbingId = responseDoc.RootElement.GetProperty(DubbingId).GetString();
float expectedDurationSeconds = responseDoc.RootElement.GetProperty(ExpectedDurationSecs).GetSingle();
return (dubbingId, expectedDurationSeconds);
using Stream responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
return await JsonSerializer.DeserializeAsync<DubbingResponse>(responseStream, cancellationToken: cancellationToken).ConfigureAwait(false);
}

private static void AppendFileToForm(MultipartFormDataContent content, string name, FileInfo fileInfo, MediaTypeHeaderValue mediaType)
Expand Down Expand Up @@ -172,7 +170,7 @@ public async Task<bool> WaitForDubbingCompletionAsync(string dubbingId, int? max
timeoutInterval ??= DefaultTimeoutInterval;
for (int i = 0; i < maxRetries; i++)
{
DubbingMetadataResponse metadata = await GetDubbingProjectMetadataAsync(dubbingId, cancellationToken).ConfigureAwait(false);
DubbingProjectMetadata metadata = await GetDubbingProjectMetadataAsync(dubbingId, cancellationToken).ConfigureAwait(false);
if (metadata.Status.Equals("dubbed", StringComparison.Ordinal))
{
return true;
Expand All @@ -192,14 +190,14 @@ public async Task<bool> WaitForDubbingCompletionAsync(string dubbingId, int? max
return false;
}

private async Task<DubbingMetadataResponse> GetDubbingProjectMetadataAsync(string dubbingId, CancellationToken cancellationToken = default)
private async Task<DubbingProjectMetadata> GetDubbingProjectMetadataAsync(string dubbingId, CancellationToken cancellationToken = default)
{
string url = $"{GetUrl()}/{dubbingId}";
HttpResponseMessage response = await client.Client.GetAsync(url, cancellationToken).ConfigureAwait(false);
await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false);
string responseBody = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
return JsonSerializer.Deserialize<DubbingMetadataResponse>(responseBody)
?? throw new JsonException("Could not deserialize the response!");
return JsonSerializer.Deserialize<DubbingProjectMetadata>(responseBody)
?? throw new JsonException("Could not deserialize the dubbing project metadata!");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

public sealed class DubbingMetadataResponse
public sealed class DubbingProjectMetadata
{
[JsonPropertyName("dubbing_id")]
public string DubbingId { get; set; }
Expand Down
12 changes: 12 additions & 0 deletions ElevenLabs-DotNet/Dubbing/DubbingResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ElevenLabs.Dubbing;

using System.Text.Json.Serialization;

public sealed class DubbingResponse
{
[JsonPropertyName("dubbing_id")]
public string DubbingId { get; set; }

[JsonPropertyName("expected_duration_sec")]
public float ExpectedDurationSeconds { get; set; }
}

0 comments on commit 348188c

Please sign in to comment.