|
| 1 | +using System.Collections.Generic; |
1 | 2 | using System.Text; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
2 | 5 |
|
3 | 6 | namespace MarkItDown.Converters; |
4 | 7 |
|
@@ -39,13 +42,19 @@ public sealed class AudioConverter : IDocumentConverter |
39 | 42 | "BitsPerSample", |
40 | 43 | }; |
41 | 44 |
|
42 | | - private readonly string? _exifToolPath; |
43 | | - private readonly Func<byte[], StreamInfo, CancellationToken, Task<string?>>? _transcribeAsync; |
| 45 | + private readonly IAudioMetadataExtractor metadataExtractor; |
| 46 | + private readonly IAudioTranscriber transcriber; |
44 | 47 |
|
45 | 48 | public AudioConverter(string? exifToolPath = null, Func<byte[], StreamInfo, CancellationToken, Task<string?>>? transcribeAsync = null) |
| 49 | + : this(new ExifToolAudioMetadataExtractor(exifToolPath), |
| 50 | + transcribeAsync is null ? NoOpAudioTranscriber.Instance : new DelegateAudioTranscriber(transcribeAsync)) |
46 | 51 | { |
47 | | - _exifToolPath = exifToolPath; |
48 | | - _transcribeAsync = transcribeAsync; |
| 52 | + } |
| 53 | + |
| 54 | + internal AudioConverter(IAudioMetadataExtractor metadataExtractor, IAudioTranscriber transcriber) |
| 55 | + { |
| 56 | + this.metadataExtractor = metadataExtractor ?? throw new ArgumentNullException(nameof(metadataExtractor)); |
| 57 | + this.transcriber = transcriber ?? throw new ArgumentNullException(nameof(transcriber)); |
49 | 58 | } |
50 | 59 |
|
51 | 60 | public int Priority => 460; |
@@ -76,7 +85,7 @@ public async Task<DocumentConverterResult> ConvertAsync(Stream stream, StreamInf |
76 | 85 | await stream.CopyToAsync(memory, cancellationToken).ConfigureAwait(false); |
77 | 86 | var bytes = memory.ToArray(); |
78 | 87 |
|
79 | | - var metadata = await ExifToolMetadataExtractor.ExtractAsync(bytes, streamInfo.Extension, _exifToolPath, cancellationToken).ConfigureAwait(false); |
| 88 | + var metadata = await metadataExtractor.ExtractAsync(bytes, streamInfo, cancellationToken).ConfigureAwait(false); |
80 | 89 | var builder = new StringBuilder(); |
81 | 90 |
|
82 | 91 | foreach (var field in MetadataFields) |
@@ -110,18 +119,65 @@ public async Task<DocumentConverterResult> ConvertAsync(Stream stream, StreamInf |
110 | 119 |
|
111 | 120 | private async Task<string?> TryTranscribeAsync(byte[] audioBytes, StreamInfo streamInfo, CancellationToken cancellationToken) |
112 | 121 | { |
113 | | - if (_transcribeAsync is null) |
| 122 | + try |
| 123 | + { |
| 124 | + return await transcriber.TranscribeAsync(audioBytes, streamInfo, cancellationToken).ConfigureAwait(false); |
| 125 | + } |
| 126 | + catch |
114 | 127 | { |
115 | 128 | return null; |
116 | 129 | } |
| 130 | + } |
117 | 131 |
|
118 | | - try |
| 132 | + internal interface IAudioMetadataExtractor |
| 133 | + { |
| 134 | + Task<IReadOnlyDictionary<string, string>> ExtractAsync(byte[] audioBytes, StreamInfo streamInfo, CancellationToken cancellationToken); |
| 135 | + } |
| 136 | + |
| 137 | + internal interface IAudioTranscriber |
| 138 | + { |
| 139 | + Task<string?> TranscribeAsync(byte[] audioBytes, StreamInfo streamInfo, CancellationToken cancellationToken); |
| 140 | + } |
| 141 | + |
| 142 | + private sealed class ExifToolAudioMetadataExtractor : IAudioMetadataExtractor |
| 143 | + { |
| 144 | + private readonly string? exifToolPath; |
| 145 | + |
| 146 | + public ExifToolAudioMetadataExtractor(string? exifToolPath) |
119 | 147 | { |
120 | | - return await _transcribeAsync(audioBytes, streamInfo, cancellationToken).ConfigureAwait(false); |
| 148 | + this.exifToolPath = exifToolPath; |
121 | 149 | } |
122 | | - catch |
| 150 | + |
| 151 | + public async Task<IReadOnlyDictionary<string, string>> ExtractAsync(byte[] audioBytes, StreamInfo streamInfo, CancellationToken cancellationToken) |
| 152 | + { |
| 153 | + var result = await ExifToolMetadataExtractor |
| 154 | + .ExtractAsync(audioBytes, streamInfo.Extension, exifToolPath, cancellationToken) |
| 155 | + .ConfigureAwait(false); |
| 156 | + |
| 157 | + return result; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private sealed class DelegateAudioTranscriber : IAudioTranscriber |
| 162 | + { |
| 163 | + private readonly Func<byte[], StreamInfo, CancellationToken, Task<string?>> factory; |
| 164 | + |
| 165 | + public DelegateAudioTranscriber(Func<byte[], StreamInfo, CancellationToken, Task<string?>> factory) |
| 166 | + => this.factory = factory; |
| 167 | + |
| 168 | + public Task<string?> TranscribeAsync(byte[] audioBytes, StreamInfo streamInfo, CancellationToken cancellationToken) |
| 169 | + => factory(audioBytes, streamInfo, cancellationToken); |
| 170 | + } |
| 171 | + |
| 172 | + private sealed class NoOpAudioTranscriber : IAudioTranscriber |
| 173 | + { |
| 174 | + public static NoOpAudioTranscriber Instance { get; } = new(); |
| 175 | + |
| 176 | + private NoOpAudioTranscriber() |
123 | 177 | { |
124 | | - return null; |
125 | 178 | } |
| 179 | + |
| 180 | + public Task<string?> TranscribeAsync(byte[] audioBytes, StreamInfo streamInfo, CancellationToken cancellationToken) |
| 181 | + => Task.FromResult<string?>(null); |
126 | 182 | } |
127 | 183 | } |
0 commit comments