2025-07-27 23:33:05 -05:00
|
|
|
using FFMpegCore;
|
|
|
|
|
using FFMpegCore.Enums;
|
|
|
|
|
using FFMpegCore.Pipes;
|
|
|
|
|
|
|
|
|
|
namespace StreamShorts.Library.Media;
|
|
|
|
|
|
2025-07-29 18:00:09 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a service for processing video files using FFMpeg.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <inheritdoc/>
|
2025-07-29 18:26:22 -05:00
|
|
|
internal sealed class FFMpegService : IVideoService
|
2025-07-27 23:33:05 -05:00
|
|
|
{
|
|
|
|
|
public async Task<bool> ExtractAudioFromVideoAsync(Stream video, Stream audio)
|
|
|
|
|
{
|
|
|
|
|
return await FFMpegArguments
|
|
|
|
|
.FromPipeInput(new StreamPipeSource(video))
|
|
|
|
|
.OutputToPipe(
|
|
|
|
|
new StreamPipeSink(audio),
|
|
|
|
|
static o => o.DisableChannel(Channel.Video).ForceFormat("mp3")
|
|
|
|
|
)
|
|
|
|
|
.ProcessAsynchronously()
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2025-08-12 00:17:37 -05:00
|
|
|
|
2025-08-19 12:43:34 -05:00
|
|
|
public async Task CreateClipFromVideoAsync(
|
2025-08-19 16:25:15 -05:00
|
|
|
string sourcePath,
|
|
|
|
|
string destinationPath,
|
2025-08-19 12:43:34 -05:00
|
|
|
TimeSpan startTime,
|
|
|
|
|
TimeSpan endTime,
|
|
|
|
|
TimeSpan? buffer = null
|
|
|
|
|
)
|
2025-08-12 00:17:37 -05:00
|
|
|
{
|
2025-08-19 12:43:34 -05:00
|
|
|
var start = startTime - (buffer ?? TimeSpan.Zero);
|
|
|
|
|
var end = endTime + (buffer ?? TimeSpan.Zero);
|
2025-08-19 16:25:15 -05:00
|
|
|
|
2025-08-19 12:43:34 -05:00
|
|
|
await FFMpeg.SubVideoAsync(sourcePath, destinationPath, start, end)
|
2025-08-12 00:17:37 -05:00
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2025-07-27 23:33:05 -05:00
|
|
|
}
|