|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import { Services } from 'components-react/service-provider'; |
| 3 | +import { $t } from 'services/i18n'; |
| 4 | +import { useVuex } from 'components-react/hooks'; |
| 5 | +import { Button, Select, Checkbox } from 'antd'; |
| 6 | +import { dialog } from '@electron/remote'; |
| 7 | +import { |
| 8 | + exportCSV, |
| 9 | + exportEDL, |
| 10 | + exportYouTubeChapters, |
| 11 | +} from 'services/highlighter/markers-exporters'; |
| 12 | +import { promises as fs } from 'fs'; |
| 13 | +const { Option } = Select; |
| 14 | + |
| 15 | +export default function ExportMarkersModal({ |
| 16 | + close, |
| 17 | + streamId, |
| 18 | +}: { |
| 19 | + close: () => void; |
| 20 | + streamId: string; |
| 21 | +}) { |
| 22 | + const { HighlighterService } = Services; |
| 23 | + const stream = useVuex(() => HighlighterService.views.highlightedStreamsDictionary[streamId]); |
| 24 | + |
| 25 | + const [markersFormat, setMarkersFormat] = useState('edl'); |
| 26 | + // many professional video editors require the timeline to start from 01:00:00:00, so we default to true |
| 27 | + const [startFromHour, setStartFromHour] = useState(true); |
| 28 | + const [exportRanges, setExportRanges] = useState(false); |
| 29 | + const [exporting, setExporting] = useState(false); |
| 30 | + |
| 31 | + const availableMarkersFormats = [ |
| 32 | + { value: 'edl', label: $t('DaVinci Resolve (EDL)') }, |
| 33 | + { value: 'csv', label: $t('CSV') }, |
| 34 | + { value: 'youtube', label: $t('YouTube Chapter Markers (for recording)') }, |
| 35 | + ]; |
| 36 | + |
| 37 | + const exportMarkers = async () => { |
| 38 | + if (stream.highlights?.length === 0) { |
| 39 | + return; |
| 40 | + } |
| 41 | + setExporting(true); |
| 42 | + |
| 43 | + try { |
| 44 | + const { filePath, canceled } = await dialog.showSaveDialog({ |
| 45 | + title: $t('Export Markers'), |
| 46 | + defaultPath: `${stream.title} - Markers.${ |
| 47 | + markersFormat === 'youtube' ? 'txt' : markersFormat |
| 48 | + }`, |
| 49 | + }); |
| 50 | + if (canceled || !filePath) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + let content = ''; |
| 55 | + if (markersFormat === 'csv') { |
| 56 | + content = await exportCSV(stream, exportRanges, startFromHour); |
| 57 | + } else if (markersFormat === 'edl') { |
| 58 | + content = await exportEDL(stream, exportRanges, startFromHour); |
| 59 | + } else if (markersFormat === 'youtube') { |
| 60 | + content = await exportYouTubeChapters(stream); |
| 61 | + } |
| 62 | + |
| 63 | + await fs.writeFile(filePath, content, 'utf-8'); |
| 64 | + close(); |
| 65 | + } finally { |
| 66 | + setExporting(false); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + return ( |
| 71 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '8px', width: '100%' }}> |
| 72 | + <h2>{$t('Export Markers')}</h2> |
| 73 | + <p>Select the video editing software you want to export markers for.</p> |
| 74 | + <Select |
| 75 | + style={{ width: '100%' }} |
| 76 | + value={markersFormat} |
| 77 | + onChange={value => setMarkersFormat(value)} |
| 78 | + > |
| 79 | + {availableMarkersFormats.map(option => ( |
| 80 | + <Option key={option.value} value={option.value}> |
| 81 | + {option.label} |
| 82 | + </Option> |
| 83 | + ))} |
| 84 | + </Select> |
| 85 | + {markersFormat !== 'youtube' && ( |
| 86 | + <Checkbox |
| 87 | + checked={startFromHour} |
| 88 | + onChange={e => setStartFromHour(e.target.checked)} |
| 89 | + style={{ marginTop: '10px' }} |
| 90 | + > |
| 91 | + {$t('Timeline starts from 01:00:00 (default)')} |
| 92 | + </Checkbox> |
| 93 | + )} |
| 94 | + |
| 95 | + {markersFormat !== 'youtube' && ( |
| 96 | + <Checkbox |
| 97 | + checked={exportRanges} |
| 98 | + onChange={e => setExportRanges(e.target.checked)} |
| 99 | + style={{ marginTop: '6px', marginLeft: '0px' }} |
| 100 | + > |
| 101 | + {$t('Export full highlight duration as marker range')} |
| 102 | + </Checkbox> |
| 103 | + )} |
| 104 | + |
| 105 | + <Button |
| 106 | + type="primary" |
| 107 | + style={{ marginTop: '20px', width: '100%' }} |
| 108 | + loading={exporting} |
| 109 | + onClick={exportMarkers} |
| 110 | + > |
| 111 | + {$t('Export')} |
| 112 | + </Button> |
| 113 | + </div> |
| 114 | + ); |
| 115 | +} |
0 commit comments