Skip to content

Commit

Permalink
refactor stream dub a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Jan 11, 2025
1 parent d803be8 commit b36b193
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
4 changes: 2 additions & 2 deletions ElevenLabs-DotNet/Dubbing/DubbingEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public async Task<DubbingProjectMetadata> DubAsync(DubbingRequest request, int?
{
if (request.Files != null)
{
foreach (var (fileName, mediaType, stream) in request.Files)
foreach (var dub in request.Files)
{
await payload.AppendFileToFormAsync("file", stream, fileName, new(mediaType), cancellationToken);
await payload.AppendFileToFormAsync("file", dub.Stream, dub.Name, new(dub.MediaType), cancellationToken);
}
}

Expand Down
14 changes: 7 additions & 7 deletions ElevenLabs-DotNet/Dubbing/DubbingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public DubbingRequest(
}

public DubbingRequest(
List<(string, string, Stream)> files,
List<DubbingStream> files,
string targetLanguage,
string sourceLanguage = null,
int? numberOfSpeakers = null,
Expand All @@ -75,7 +75,7 @@ public DubbingRequest(
private DubbingRequest(
string targetLanguage,
Uri sourceUrl = null,
List<(string, string, Stream)> files = null,
List<DubbingStream> files = null,
IEnumerable<string> filePaths = null,
string sourceLanguage = null,
int? numberOfSpeakers = null,
Expand Down Expand Up @@ -130,7 +130,7 @@ private DubbingRequest(
".webm" => "video/webm",
_ => "application/octet-stream"
};
files.Add((fileInfo.Name, mediaType, stream));
files.Add(new(stream, fileInfo.Name, mediaType));
}
}

Expand All @@ -152,7 +152,7 @@ private DubbingRequest(
/// <summary>
/// Files to dub.
/// </summary>
public IReadOnlyList<(string, string, Stream)> Files { get; }
public IReadOnlyList<DubbingStream> Files { get; }

/// <summary>
/// URL of the source video/audio file.
Expand Down Expand Up @@ -221,12 +221,12 @@ private void Dispose(bool disposing)
if (disposing)
{
if (Files == null) { return; }
foreach (var (_, _, stream) in Files)

foreach (var dub in Files)
{
try
{
stream?.Close();
stream?.Dispose();
dub.Dispose();
}
catch (Exception e)
{
Expand Down
64 changes: 64 additions & 0 deletions ElevenLabs-DotNet/Dubbing/DubbingStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.IO;

namespace ElevenLabs.Dubbing
{
public sealed class DubbingStream : IDisposable
{
public DubbingStream(Stream stream, string name, string mediaType)
{
Stream = stream ?? throw new ArgumentNullException(nameof(stream));

if (Stream.Length == 0)
{
throw new ArgumentException("Stream cannot be empty.");
}

if (!Stream.CanRead)
{
throw new ArgumentException("Stream must be readable.");
}

Name = name ?? throw new ArgumentNullException(nameof(name));

if (string.IsNullOrWhiteSpace(Name))
{
throw new ArgumentException("Name cannot be empty.");
}

MediaType = mediaType ?? throw new ArgumentNullException(nameof(mediaType));

if (string.IsNullOrWhiteSpace(MediaType))
{
throw new ArgumentException("Media type cannot be empty.");
}

if (MediaType.Contains("/"))
{
var parts = MediaType.Split('/');

if (parts.Length != 2 || string.IsNullOrWhiteSpace(parts[0]) || string.IsNullOrWhiteSpace(parts[1]))
{
throw new ArgumentException("Invalid media type.");
}
}
else
{
throw new ArgumentException("Invalid media type.");
}
}

public Stream Stream { get; }

public string Name { get; }

public string MediaType { get; }

public void Dispose()
{
Stream?.Dispose();
}
}
}
1 change: 1 addition & 0 deletions ElevenLabs-DotNet/ElevenLabs-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ All copyrights, trademarks, logos, and assets are the property of their respecti
<PackageReleaseNotes>
Version 3.4.2
- Added flash models
- Added stream input support to dubbing endpoint
- Fixed http/https protocol in client settings
Version 3.4.1
- Removed text length check in TextToSpeechRequest
Expand Down

0 comments on commit b36b193

Please sign in to comment.