一个非官方的11个实验室语音综合静止客户端。
我不隶属于Elevenlabs,需要具有API访问的帐户。
所有版权,商标,徽标和资产都是其各自所有者的财产。
从Nuget安装包裹ElevenLabs-DotNet 。这是通过命令行的方式:
Install-Package ElevenLabs - DotNet dotnet add package ElevenLabs-DotNet
想要在Unity游戏引擎中使用Elevenlabs?在OpenUpm上查看我们的Unity软件包:
有3种提供您的API键的方法,按优先顺序:
var api = new ElevenLabsClient ( "yourApiKey" ) ;或手动创建ElevenLabsAuthentication对象
var api = new ElevenLabsClient ( new ElevenLabsAuthentication ( "yourApiKey" ) ) ; 尝试从配置文件,默认情况下加载API键,默认.elevenlabs下。
要创建一个配置文件,请创建一个名为.elevenlabs的新文本文件,并包含该行:
{
"apiKey" : " yourApiKey " ,
}您还可以通过调用身份验证中的静态方法直接加载文件:
var api = new ElevenLabsClient ( ElevenLabsAuthentication . LoadFromDirectory ( "your/path/to/.elevenlabs" ) ) ; ; 使用系统的环境变量指定使用的API键。
ELEVEN_LABS_API_KEY用于API键。 var api = new ElevenLabsClient ( ElevenLabsAuthentication . LoadFromEnv ( ) ) ;直接在前端应用程序中使用ElevenLabs-dotnet或com.rest.Rest.REST.REST.REST.REST.EREVENLABS软件包可能会暴露您的API键和其他敏感信息。为了减轻这种风险,建议建立一个中间API,该API代表您的前端应用程序向Elevenlab提出请求。该库可以用于前端和中间主机配置,以确保与Elevenlabs API的安全通信。
在前端示例中,您需要使用首选的OAuth提供商对用户进行安全身份验证。对用户进行身份验证后,将您的自定义验证令牌与后端上的API密钥交换。
请按照以下步骤:
ElevenLabsAuthentication对象并传递自定义令牌。ElevenLabsClientSettings对象,并指定中间API所在的域。auth和settings对象传递到ElevenLabsClient构造函数。这是如何设置前端的一个示例:
var authToken = await LoginAsync ( ) ;
var auth = new ElevenLabsAuthentication ( authToken ) ;
var settings = new ElevenLabsClientSettings ( domain : "api.your-custom-domain.com" ) ;
var api = new ElevenLabsClient ( auth , settings ) ;此设置允许您的前端应用程序与您的后端安全通信,该后端将使用Elevenlabs-dotnet-Proxy,然后将请求转发到Eleevenlabs API。这样可以确保您的Elevenlabs API密钥和其他敏感信息在整个过程中保持安全。
在此示例中,我们演示了如何在新的ASP.NET Core Web应用程序中设置和使用ElevenLabsProxyStartup 。代理服务器将处理身份验证和转发请求到ElevenLabs API,以确保您的API键和其他敏感信息保持安全。
Install-Package ElevenLabs-DotNet-Proxydotnet add package ElevenLabs-DotNet-Proxy<PackageReference Include="ElevenLabs-DotNet-Proxy" />AbstractAuthenticationFilter继承的新类,并覆盖ValidateAuthentication方法。这将实现您将用于检查内部服务器的用户IAuthenticationFilter令牌。Program.cs中,通过调用ElevenLabsProxyStartup.CreateWebApplication方法来创建一个新的代理Web应用程序,将您的自定义AuthenticationFilter作为类型参数传递。ElevenLabsAuthentication和ElevenLabsClientSettings 。 public partial class Program
{
private class AuthenticationFilter : AbstractAuthenticationFilter
{
public override async Task ValidateAuthenticationAsync ( IHeaderDictionary request )
{
await Task . CompletedTask ; // remote resource call
// You will need to implement your own class to properly test
// custom issued tokens you've setup for your end users.
if ( ! request [ "xi-api-key" ] . ToString ( ) . Contains ( TestUserToken ) )
{
throw new AuthenticationException ( "User is not authorized" ) ;
}
}
}
public static void Main ( string [ ] args )
{
var auth = ElevenLabsAuthentication . LoadFromEnv ( ) ;
var client = new ElevenLabsClient ( auth ) ;
ElevenLabsProxyStartup . CreateWebApplication < AuthenticationFilter > ( args , client ) . Run ( ) ;
}
}设置代理服务器后,您的最终用户现在可以向代理API而不是直接向ElevenLabs API提出身份验证的请求。代理服务器将处理身份验证和转发请求到ElevenLabs API,以确保您的API键和其他敏感信息保持安全。
将文本转换为语音。
var api = new ElevenLabsClient ( ) ;
var text = "The quick brown fox jumps over the lazy dog." ;
var voice = ( await api . VoicesEndpoint . GetAllVoicesAsync ( ) ) . FirstOrDefault ( ) ;
var request = new TextToSpeechRequest ( voice , text ) ;
var voiceClip = await api . TextToSpeechEndpoint . TextToSpeechAsync ( request ) ;
await File . WriteAllBytesAsync ( $ " { voiceClip . Id } .mp3" , voiceClip . ClipData . ToArray ( ) ) ; 将文字流到语音。
var api = new ElevenLabsClient ( ) ;
var text = "The quick brown fox jumps over the lazy dog." ;
var voice = ( await api . VoicesEndpoint . GetAllVoicesAsync ( ) ) . FirstOrDefault ( ) ;
string fileName = "myfile.mp3" ;
using var outputFileStream = File . OpenWrite ( fileName ) ;
var request = new TextToSpeechRequest ( voice , text ) ;
var voiceClip = await api . TextToSpeechEndpoint . TextToSpeechAsync ( request ,
partialClipCallback : async ( partialClip ) =>
{
// Write the incoming data to the output file stream.
// Alternatively you can play this clip data directly.
await outputFileStream . WriteAsync ( partialClip . ClipData ) ;
} ) ;访问用户或Elevenlab创建的声音。
在公共语音库中获取共享声音列表。
var api = new ElevenLabsClient ( ) ;
var results = await ElevenLabsClient . SharedVoicesEndpoint . GetSharedVoicesAsync ( ) ;
foreach ( var voice in results . Voices )
{
Console . WriteLine ( $ " { voice . OwnerId } | { voice . VoiceId } | { voice . Date } | { voice . Name } " ) ;
} 获取您帐户可用的所有可用声音的列表。
var api = new ElevenLabsClient ( ) ;
var allVoices = await api . VoicesEndpoint . GetAllVoicesAsync ( ) ;
foreach ( var voice in allVoices )
{
Console . WriteLine ( $ " { voice . Id } | { voice . Name } | similarity boost: { voice . Settings ? . SimilarityBoost } | stability: { voice . Settings ? . Stability } " ) ;
} 获取全局默认语音设置。
var api = new ElevenLabsClient ( ) ;
var result = await api . VoicesEndpoint . GetDefaultVoiceSettingsAsync ( ) ;
Console . WriteLine ( $ "stability: { result . Stability } | similarity boost: { result . SimilarityBoost } " ) ; var api = new ElevenLabsClient ( ) ;
var voice = await api . VoicesEndpoint . GetVoiceAsync ( "voiceId" ) ;
Console . WriteLine ( $ " { voice . Id } | { voice . Name } | { voice . PreviewUrl } " ) ; 为特定语音编辑设置。
var api = new ElevenLabsClient ( ) ;
var success = await api . VoicesEndpoint . EditVoiceSettingsAsync ( voice , new VoiceSettings ( 0.7f , 0.7f ) ) ;
Console . WriteLine ( $ "Was successful? { success } " ) ; var api = new ElevenLabsClient ( ) ;
var labels = new Dictionary < string , string >
{
{ "accent" , "american" }
} ;
var audioSamplePaths = new List < string > ( ) ;
var voice = await api . VoicesEndpoint . AddVoiceAsync ( "Voice Name" , audioSamplePaths , labels ) ; var api = new ElevenLabsClient ( ) ;
var labels = new Dictionary < string , string >
{
{ "age" , "young" }
} ;
var audioSamplePaths = new List < string > ( ) ;
var success = await api . VoicesEndpoint . EditVoiceAsync ( voice , audioSamplePaths , labels ) ;
Console . WriteLine ( $ "Was successful? { success } " ) ; var api = new ElevenLabsClient ( ) ;
var success = await api . VoicesEndpoint . DeleteVoiceAsync ( voiceId ) ;
Console . WriteLine ( $ "Was successful? { success } " ) ; 访问您的样本,克隆声音时创建的样本。
var api = new ElevenLabsClient ( ) ;
var voiceClip = await api . VoicesEndpoint . DownloadVoiceSampleAsync ( voice , sample ) ;
await File . WriteAllBytesAsync ( $ " { voiceClip . Id } .mp3" , voiceClip . ClipData . ToArray ( ) ) ; var api = new ElevenLabsClient ( ) ;
var success = await api . VoicesEndpoint . DeleteVoiceSampleAsync ( voiceId , sampleId ) ;
Console . WriteLine ( $ "Was successful? { success } " ) ;配音将音频或视频文件提供给给定的语言。
var api = new ElevenLabsClient ( ) ;
// from URI
var request = new DubbingRequest ( new Uri ( "https://youtu.be/Zo5-rhYOlNk" ) , "ja" , "en" , 1 , true ) ;
// from file
var request = new DubbingRequest ( filePath , "es" , "en" , 1 ) ;
var metadata = await api . DubbingEndpoint . DubAsync ( request , progress : new Progress < DubbingProjectMetadata > ( metadata =>
{
switch ( metadata . Status )
{
case "dubbing" :
Console . WriteLine ( $ "Dubbing for { metadata . DubbingId } in progress... Expected Duration: { metadata . ExpectedDurationSeconds : 0.00 } seconds" ) ;
break ;
case "dubbed" :
Console . WriteLine ( $ "Dubbing for { metadata . DubbingId } complete in { metadata . TimeCompleted . TotalSeconds : 0.00 } seconds!" ) ;
break ;
default :
Console . WriteLine ( $ "Status: { metadata . Status } " ) ;
break ;
}
} ) ) ; 返回有关配音项目的元数据,包括是否仍在进行中。
var api = new ElevenLabsClient ( ) ;
var metadata = api . await GetDubbingProjectMetadataAsync ( "dubbing - id");将称为配音文件作为流文件返回。
笔记
视频将以MP4格式返回,并且音频只会在MP3中返回配音。
var assetsDir = Path . GetFullPath ( "../../../Assets" ) ;
var dubbedPath = new FileInfo ( Path . Combine ( assetsDir , $ "online.dubbed. { request . TargetLanguage } .mp4" ) ) ;
{
await using var fs = File . Open ( dubbedPath . FullName , FileMode . Create ) ;
await foreach ( var chunk in ElevenLabsClient . DubbingEndpoint . GetDubbedFileAsync ( metadata . DubbingId , request . TargetLanguage ) )
{
await fs . WriteAsync ( chunk ) ;
}
} 返回所需格式的配音的转录本。
var assetsDir = Path . GetFullPath ( "../../../Assets" ) ;
var transcriptPath = new FileInfo ( Path . Combine ( assetsDir , $ "online.dubbed. { request . TargetLanguage } .srt" ) ) ;
{
var transcriptFile = await api . DubbingEndpoint . GetTranscriptForDubAsync ( metadata . DubbingId , request . TargetLanguage ) ;
await File . WriteAllTextAsync ( transcriptPath . FullName , transcriptFile ) ;
} 删除配音项目。
var api = new ElevenLabsClient ( ) ;
await api . DubbingEndpoint . DeleteDubbingProjectAsync ( "dubbing-id" ) ;将文本转换为声音并使用有史以来最高级的AI音频模型的API。
var api = new ElevenLabsClient ( ) ;
var request = new SoundGenerationRequest ( "Star Wars Light Saber parry" ) ;
var clip = await api . SoundGenerationEndpoint . GenerateSoundAsync ( request ) ;访问您先前合成的音频剪辑,包括其元数据。
获取有关您生成的所有音频的元数据。
var api = new ElevenLabsClient ( ) ;
var historyItems = await api . HistoryEndpoint . GetHistoryAsync ( ) ;
foreach ( var item in historyItems . OrderBy ( historyItem => historyItem . Date ) )
{
Console . WriteLine ( $ " { item . State } { item . Date } | { item . Id } | { item . Text . Length } | { item . Text } " ) ;
} 获取有关特定项目的信息。
var api = new ElevenLabsClient ( ) ;
var historyItem = await api . HistoryEndpoint . GetHistoryItemAsync ( voiceClip . Id ) ; var api = new ElevenLabsClient ( ) ;
var voiceClip = await api . HistoryEndpoint . DownloadHistoryAudioAsync ( historyItem ) ;
await File . WriteAllBytesAsync ( $ " { voiceClip . Id } .mp3" , voiceClip . ClipData . ToArray ( ) ) ; 下载最后100个历史记录项目或指定项目的收集。
var api = new ElevenLabsClient ( ) ;
var voiceClips = await api . HistoryEndpoint . DownloadHistoryItemsAsync ( ) ; var api = new ElevenLabsClient ( ) ;
var success = await api . HistoryEndpoint . DeleteHistoryItemAsync ( historyItem ) ;
Console . WriteLine ( $ "Was successful? { success } " ) ;访问您的用户信息和订阅状态。
通过ElevenLabs获取有关您的用户帐户的信息。
var api = new ElevenLabsClient ( ) ;
var userInfo = await api . UserEndpoint . GetUserInfoAsync ( ) ; 获取有关您使用ElevenLabs订阅的信息。
var api = new ElevenLabsClient ( ) ;
var subscriptionInfo = await api . UserEndpoint . GetSubscriptionInfoAsync ( ) ;