( transcript: TranscriptSegment[], options: TranscriptExportOptions )
| 268 | } |
| 269 | |
| 270 | export function createTranscriptExport( |
| 271 | transcript: TranscriptSegment[], |
| 272 | options: TranscriptExportOptions |
| 273 | ): TranscriptExportResult { |
| 274 | if (!Array.isArray(transcript) || transcript.length === 0) { |
| 275 | throw new Error('Transcript is empty.'); |
| 276 | } |
| 277 | |
| 278 | const hasSpeakerLabels = transcript.some((segment) => Boolean(getSegmentSpeaker(segment))); |
| 279 | const normalizedOptions: TranscriptExportOptions = { |
| 280 | ...options, |
| 281 | includeSpeakers: options.includeSpeakers && hasSpeakerLabels, |
| 282 | }; |
| 283 | |
| 284 | let content: string; |
| 285 | let extension: string; |
| 286 | let mimeType: string; |
| 287 | |
| 288 | switch (options.format) { |
| 289 | case 'srt': |
| 290 | content = generateSrtContent(transcript, normalizedOptions, hasSpeakerLabels); |
| 291 | extension = 'srt'; |
| 292 | mimeType = 'text/plain;charset=utf-8'; |
| 293 | break; |
| 294 | case 'csv': |
| 295 | content = generateCsvContent(transcript, normalizedOptions, hasSpeakerLabels); |
| 296 | extension = 'csv'; |
| 297 | mimeType = 'text/csv;charset=utf-8'; |
| 298 | break; |
| 299 | case 'txt': |
| 300 | default: |
| 301 | content = generateTxtContent(transcript, normalizedOptions, hasSpeakerLabels); |
| 302 | extension = 'txt'; |
| 303 | mimeType = 'text/plain;charset=utf-8'; |
| 304 | } |
| 305 | |
| 306 | const filenameParts = [DEFAULT_FILENAME]; |
| 307 | if (options.videoTitle) { |
| 308 | filenameParts.push( |
| 309 | options.videoTitle |
| 310 | .toLowerCase() |
| 311 | .replace(/[^a-z0-9]+/gi, '-') |
| 312 | .replace(/^-+|-+$/g, '') |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | if (options.exportMode && options.exportMode !== 'original') { |
| 317 | filenameParts.push(options.exportMode); |
| 318 | } |
| 319 | |
| 320 | const filename = `${filenameParts.join('-') || DEFAULT_FILENAME}.${extension}`; |
| 321 | const blob = new Blob([content], { type: mimeType }); |
| 322 | |
| 323 | return { blob, filename }; |
| 324 | } |
| 325 | |
| 326 | export function hasSpeakerMetadata(transcript: TranscriptSegment[]): boolean { |
| 327 | return transcript.some((segment) => Boolean(getSegmentSpeaker(segment))); |
no test coverage detected