Added TTS
This commit is contained in:
30
Astrocore.Api/.dockerignore
Normal file
30
Astrocore.Api/.dockerignore
Normal file
@@ -0,0 +1,30 @@
|
||||
**/.classpath
|
||||
**/.dockerignore
|
||||
**/.env
|
||||
**/.git
|
||||
**/.gitignore
|
||||
**/.project
|
||||
**/.settings
|
||||
**/.toolstarget
|
||||
**/.vs
|
||||
**/.vscode
|
||||
**/*.*proj.user
|
||||
**/*.dbmdl
|
||||
**/*.jfm
|
||||
**/azds.yaml
|
||||
**/bin
|
||||
**/charts
|
||||
**/docker-compose*
|
||||
**/Dockerfile*
|
||||
**/node_modules
|
||||
**/npm-debug.log
|
||||
**/obj
|
||||
**/secrets.dev.yaml
|
||||
**/values.dev.yaml
|
||||
LICENSE
|
||||
README.md
|
||||
!**/.gitignore
|
||||
!.git/HEAD
|
||||
!.git/config
|
||||
!.git/packed-refs
|
||||
!.git/refs/heads/**
|
||||
@@ -6,9 +6,12 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>916bd77d-132b-42cb-93e0-55d05b0d4f4b</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<DockerfileContext>.</DockerfileContext>
|
||||
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FFMpegCore" Version="5.2.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
67
Astrocore.Api/Controllers/TextToSpeechController.cs
Normal file
67
Astrocore.Api/Controllers/TextToSpeechController.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Astrocore.Api.Controllers
|
||||
{
|
||||
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class TextToSpeechController : ControllerBase
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
public TextToSpeechController(IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index(string message)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
var body = $"msg={Uri.EscapeDataString(message)}&lang=Gwyneth&source=ttsmp3";
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "https://ttsmp3.com/makemp3_new.php")
|
||||
{
|
||||
Content = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded")
|
||||
};
|
||||
|
||||
var response = await client.SendAsync(request);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var mp3File = doc.RootElement.GetProperty("URL").GetString();
|
||||
var mp3Bytes = await client.GetByteArrayAsync(mp3File);
|
||||
var tempDir = Path.GetTempPath();
|
||||
var mp3Path = Path.Combine(tempDir, "tts.mp3");
|
||||
var dfpwmPath = Path.Combine(tempDir, "tts.dfpwm");
|
||||
|
||||
await System.IO.File.WriteAllBytesAsync(mp3Path, mp3Bytes);
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "ffmpeg",
|
||||
Arguments = $"-i \"{mp3Path}\" -ac 1 -c:a dfpwm -ar 48k \"{dfpwmPath}\"",
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
string errorOutput = await process.StandardError.ReadToEndAsync();
|
||||
process.WaitForExit();
|
||||
|
||||
if (!System.IO.File.Exists(dfpwmPath))
|
||||
{
|
||||
return StatusCode(500, $"FFmpeg failed: {errorOutput}");
|
||||
|
||||
}
|
||||
|
||||
var dfpwmBytes = await System.IO.File.ReadAllBytesAsync(dfpwmPath);
|
||||
return File(dfpwmBytes, "application/octet-stream", "tts.dfpwm");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,15 @@
|
||||
|
||||
# This stage is used when running from VS in fast mode (Default for Debug configuration)
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
|
||||
# Install ffmpeg before switching user
|
||||
RUN apt-get update && \
|
||||
apt-get install -y ffmpeg && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
USER $APP_UID
|
||||
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
@@ -26,5 +33,11 @@ RUN dotnet publish "./Astrocore.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publ
|
||||
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
|
||||
# Install ffmpeg
|
||||
RUN apt-get update && \
|
||||
apt-get install -y ffmpeg && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "Astrocore.Api.dll"]
|
||||
ENTRYPOINT ["dotnet", "Astrocore.Api.dll"]
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Astrocore.Api
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddHttpClient();
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
@@ -23,8 +23,8 @@ namespace Astrocore.Api
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
//app.UseHttpsRedirection();
|
||||
//app.UseAuthorization();
|
||||
app.UseHttpsRedirection();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user