一個非官方的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 ( ) ;