Initial commit: modern React video player library
Add all source files for a feature-rich, reusable video player built with React, TypeScript, and Vite. Includes core components, context, hooks, utilities, styles, demo app, and configuration files.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Parse SRT subtitle format to WebVTT
|
||||
*/
|
||||
export const parseSRT = (srtContent: string): string => {
|
||||
const lines = srtContent.trim().split('\n')
|
||||
let vttContent = 'WEBVTT\n\n'
|
||||
|
||||
let i = 0
|
||||
while (i < lines.length) {
|
||||
// Skip subtitle number
|
||||
if (/^\d+$/.test(lines[i].trim())) {
|
||||
i++
|
||||
}
|
||||
|
||||
// Parse timestamp line
|
||||
if (lines[i] && lines[i].includes('-->')) {
|
||||
const timeLine = lines[i].replace(/,/g, '.') // SRT uses comma, VTT uses dot
|
||||
vttContent += timeLine + '\n'
|
||||
i++
|
||||
|
||||
// Add subtitle text
|
||||
while (i < lines.length && lines[i].trim() !== '') {
|
||||
vttContent += lines[i] + '\n'
|
||||
i++
|
||||
}
|
||||
vttContent += '\n'
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
return vttContent
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a blob URL from subtitle content
|
||||
*/
|
||||
export const createSubtitleBlobURL = (content: string, format: 'vtt' | 'srt'): string => {
|
||||
const vttContent = format === 'srt' ? parseSRT(content) : content
|
||||
const blob = new Blob([vttContent], { type: 'text/vtt' })
|
||||
return URL.createObjectURL(blob)
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and parse subtitle file
|
||||
*/
|
||||
export const fetchSubtitle = async (url: string): Promise<string> => {
|
||||
try {
|
||||
const response = await fetch(url)
|
||||
const content = await response.text()
|
||||
|
||||
// Detect format
|
||||
if (url.endsWith('.srt')) {
|
||||
return parseSRT(content)
|
||||
}
|
||||
|
||||
return content
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch subtitle:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user