mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-19 19:51:20 -08:00
添加ass格式字幕支持
This commit is contained in:
1 parent
1644a7530f
commit
2fbacf77a6
5 files changed
+132
-16
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { setStyles, srtToVtt, vttToBlob } from './utils';
|
||||
import { setStyles, srtToVtt, vttToBlob, getExt } from './utils';
|
||||
import assToVtt from './utils/assToVtt';
|
||||
|
||||
export default class Subtitle {
|
||||
constructor(art) {
|
||||
@@ -52,7 +53,7 @@ export default class Subtitle {
|
||||
this.isInit = true;
|
||||
proxy(track, 'cuechange', updateSubtitle.bind(this));
|
||||
}
|
||||
|
||||
|
||||
this.art.on('artplayerPluginSubtitle:set', updateSubtitle.bind(this));
|
||||
}
|
||||
});
|
||||
@@ -60,15 +61,18 @@ export default class Subtitle {
|
||||
|
||||
load(url) {
|
||||
const { notice } = this.art;
|
||||
let type;
|
||||
return fetch(url)
|
||||
.then(response => {
|
||||
type = response.headers.get('Content-Type');
|
||||
return response.text();
|
||||
})
|
||||
.then(text => {
|
||||
if (/x-subrip/gi.test(type)) {
|
||||
return vttToBlob(srtToVtt(text));
|
||||
const type = getExt(url);
|
||||
const formatText = text.replace(/{[\s\S]*?}/g, '');
|
||||
if (type === 'srt') {
|
||||
return vttToBlob(srtToVtt(formatText));
|
||||
}
|
||||
if (type === 'ass') {
|
||||
return vttToBlob(assToVtt(formatText));
|
||||
}
|
||||
return url;
|
||||
})
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
export default function assToVtt(ass) {
|
||||
const reAss = new RegExp(
|
||||
'Dialogue:\\s\\d,' +
|
||||
'(\\d+:\\d\\d:\\d\\d.\\d\\d),' +
|
||||
'(\\d+:\\d\\d:\\d\\d.\\d\\d),' +
|
||||
'([^,]*),' +
|
||||
'([^,]*),' +
|
||||
'(?:[^,]*,){4}' +
|
||||
'([\\s\\S]*)$',
|
||||
'i',
|
||||
);
|
||||
|
||||
function fixTime(time = '') {
|
||||
return time
|
||||
.split(/[:.]/)
|
||||
.map((item, index, arr) => {
|
||||
if (index === arr.length - 1) {
|
||||
if (item.length === 1) {
|
||||
return `.${item}00`;
|
||||
}
|
||||
|
||||
if (item.length === 2) {
|
||||
return `.${item}0`;
|
||||
}
|
||||
} else if (item.length === 1) {
|
||||
return (index === 0 ? '0' : ':0') + item;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-nested-ternary
|
||||
return index === 0 ? item : index === arr.length - 1 ? `.${item}` : `:${item}`;
|
||||
})
|
||||
.join('');
|
||||
}
|
||||
|
||||
return `WEBVTT\n\n${ass
|
||||
.split(/\r?\n/)
|
||||
.map(line => {
|
||||
const m = line.match(reAss);
|
||||
if (!m) return null;
|
||||
return {
|
||||
start: fixTime(m[1].trim()),
|
||||
end: fixTime(m[2].trim()),
|
||||
text: m[5]
|
||||
.replace(/{[\s\S]*?}/g, '')
|
||||
.replace(/(\\N)/g, '\n')
|
||||
.trim()
|
||||
.split(/\r?\n/)
|
||||
.map(item => item.trim())
|
||||
.join('\n'),
|
||||
};
|
||||
})
|
||||
.filter(line => line)
|
||||
.map((line, index) => {
|
||||
if (line) {
|
||||
return `${index + 1}\n${line.start} --> ${line.end}\n${line.text}`;
|
||||
}
|
||||
return '';
|
||||
})
|
||||
.filter(line => line.trim())
|
||||
.join('\n\n')}`;
|
||||
}
|
||||
Reference in new issue
Block a user