@@ -954,6 +954,102 @@ export async function processImage(vod_pic, moduleObject, injectVars = null) {
954954 return vod_pic ;
955955}
956956
957+ // 格式化时间为SRT格式 HH:MM:SS,mmm
958+ function formatSrtTime ( milliseconds ) {
959+ const hours = Math . floor ( milliseconds / 3600000 ) ;
960+ const minutes = Math . floor ( ( milliseconds % 3600000 ) / 60000 ) ;
961+ const seconds = Math . floor ( ( milliseconds % 60000 ) / 1000 ) ;
962+ const ms = milliseconds % 1000 ;
963+
964+ return `${ hours . toString ( ) . padStart ( 2 , '0' ) } :${ minutes . toString ( ) . padStart ( 2 , '0' ) } :${ seconds . toString ( ) . padStart ( 2 , '0' ) } ,${ ms . toString ( ) . padStart ( 3 , '0' ) } ` ;
965+ }
966+
967+ // LRC格式歌词转SRT字幕格式
968+ export function lrcToSrt ( lrcContent ) {
969+ if ( ! lrcContent || typeof lrcContent !== 'string' ) {
970+ return '' ;
971+ }
972+
973+ // 解析LRC歌词行
974+ const lines = lrcContent . split ( '\n' ) ;
975+ const timeLines = [ ] ;
976+
977+ for ( const line of lines ) {
978+ const trimmedLine = line . trim ( ) ;
979+ if ( ! trimmedLine ) continue ;
980+
981+ // 匹配时间标签格式 [mm:ss.xx] 或 [mm:ss]
982+ const timeMatch = trimmedLine . match ( / ^ \[ ( \d { 1 , 2 } ) : ( \d { 2 } ) (?: \. ( \d { 1 , 3 } ) ) ? \] ( .* ) $ / ) ;
983+ if ( timeMatch ) {
984+ const minutes = parseInt ( timeMatch [ 1 ] ) ;
985+ const seconds = parseInt ( timeMatch [ 2 ] ) ;
986+ const milliseconds = timeMatch [ 3 ] ? parseInt ( timeMatch [ 3 ] . padEnd ( 3 , '0' ) ) : 0 ;
987+ const text = timeMatch [ 4 ] . trim ( ) ;
988+
989+ // 计算总毫秒数
990+ const totalMs = minutes * 60 * 1000 + seconds * 1000 + milliseconds ;
991+
992+ if ( text ) { // 只添加有文本内容的行
993+ timeLines . push ( {
994+ time : totalMs ,
995+ text : text
996+ } ) ;
997+ }
998+ }
999+ }
1000+
1001+ // 按时间排序
1002+ timeLines . sort ( ( a , b ) => a . time - b . time ) ;
1003+
1004+ if ( timeLines . length === 0 ) {
1005+ return '' ;
1006+ }
1007+
1008+ // 转换为SRT格式
1009+ let srtContent = '' ;
1010+ for ( let i = 0 ; i < timeLines . length ; i ++ ) {
1011+ const currentLine = timeLines [ i ] ;
1012+ const nextLine = timeLines [ i + 1 ] ;
1013+
1014+ // 计算结束时间(下一行的开始时间,或当前时间+3秒)
1015+ const endTime = nextLine ? nextLine . time : currentLine . time + 3000 ;
1016+
1017+ // 格式化时间为SRT格式 HH:MM:SS,mmm
1018+ const startTimeStr = formatSrtTime ( currentLine . time ) ;
1019+ const endTimeStr = formatSrtTime ( endTime ) ;
1020+
1021+ // 添加SRT条目
1022+ srtContent += `${ i + 1 } \n` ;
1023+ srtContent += `${ startTimeStr } --> ${ endTimeStr } \n` ;
1024+ srtContent += `${ currentLine . text } \n\n` ;
1025+ }
1026+
1027+ return srtContent . trim ( ) ;
1028+ }
1029+
1030+ /**
1031+ * 字符串正则表达式提取函数
1032+ * @param {string } content - 要搜索的源字符串
1033+ * @param {string } pattern - 正则表达式模式
1034+ * @param {number } groupIndex - 捕获组索引,0表示整个匹配,1表示第一个捕获组,以此类推
1035+ * @returns {string } 提取到的字符串,如果没有匹配则返回空字符串
1036+ */
1037+ export function strExtract ( content , pattern , groupIndex = 0 ) {
1038+ try {
1039+ const regex = new RegExp ( pattern ) ;
1040+ const match = content . match ( regex ) ;
1041+
1042+ if ( match && match [ groupIndex ] !== undefined ) {
1043+ return match [ groupIndex ] ;
1044+ }
1045+
1046+ return '' ;
1047+ } catch ( error ) {
1048+ console . error ( 'strExtract error:' , error ) ;
1049+ return '' ;
1050+ }
1051+ }
1052+
9571053export const pako = globalThis . pako ;
9581054export const gbkTool = globalThis . gbkTool ;
9591055export const JSEncrypt = globalThis . JSEncrypt ;
@@ -970,4 +1066,6 @@ globalThis.base64Encode = base64Encode;
9701066globalThis . base64Decode = base64Decode ;
9711067globalThis . gzip = gzip ;
9721068globalThis . ungzip = ungzip ;
973- globalThis . parseQueryString = parseQueryString ;
1069+ globalThis . parseQueryString = parseQueryString ;
1070+ globalThis . lrcToSrt = lrcToSrt ;
1071+ globalThis . strExtract = strExtract ;
0 commit comments