DVideo + vue3 使用 帮助文档 https://dplayer.diygod.dev/zh/guide.html#special-thanks
安装 1 2 pnpm install dplayer --save pnpm install @types/dplayer --save-dev
使用方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 <script setup lang="ts"> import { ref, onMounted, onBeforeUnmount } from 'vue'; import DPlayer from 'dplayer'; import type { DPlayerOptions, DPlayerVideoOptions, DPlayerDanmakuOptions, DPlayerSubtitleOptions } from '@/interfaces/DPlayerOptions.ts'; const prop = defineProps<{ url: string; pic?: string; subtitle?: string; autoplay: boolean; danmakuId?: string; danmakuApi?: string; }>() /** * 视频配置属性 */ const videoOptions: DPlayerVideoOptions = { url: prop.url, pic: prop.pic ? prop.pic : undefined, thumbnails: prop.pic ? prop.pic : undefined, }; /** * 弹幕配置属性 */ const danmakuOptions: DPlayerDanmakuOptions = { id: prop.danmakuId ? prop.danmakuId : 'no danmaku', api: prop.danmakuApi ? prop.danmakuApi : 'no danmaku' } /** * 字幕配置属性 */ const subtitleOptions: DPlayerSubtitleOptions = { url: prop.subtitle ? prop.subtitle : 'no subtitle', type: 'webvtt', bottom:'20%', fontSize: '32px', } /** * DPlayer 配置属性 */ const playerOptions: DPlayerOptions = { container: null, autoplay: prop.autoplay, video: videoOptions, danmaku: prop.danmakuId ? danmakuOptions : undefined, subtitle: prop.subtitle ? subtitleOptions : undefined }; const dplayerContainer = ref(null); // 引用视频播放器的容器元素 const dplayerInstance = ref<DPlayer | null>(null); // DPlayer 实例 onMounted(() => { if (dplayerContainer.value) { // 检查 dplayerContainer 是否已绑定到实际的 DOM 元素上 playerOptions.container = dplayerContainer.value; // 将容器元素绑定到 DPlayer 配置中 dplayerInstance.value = new DPlayer(playerOptions); // 创建 DPlayer 实例 } }); onBeforeUnmount(() => { // 在组件销毁前销毁 DPlayer 实例,避免内存泄漏 if (dplayerInstance.value) { dplayerInstance.value.destroy(); // 销毁 DPlayer 实例 } }); </script> <template> <div class="dplayer-container" ref="dplayerContainer" /> </template> <style scoped lang="scss"> .dplayer-container { width: 100%; height: 500px; /* Adjust based on your needs */ } </style>
属性配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 import type { SubTitleType } from "dplayer" ; export interface DPlayerVideoOptions { url : string ; pic ?: string ; thumbnails ?: string ; } export interface DanmakuAPIBackend { read : (endpoint : { url: string }, callback : (data: any ) => void ) => void ; send : ( endpoint : { url: string }, danmakuData : any , callback : () => void ) => void ;} export interface DPlayerDanmakuOptions { id : string ; api : string ; token ?: string ; maximum ?: string ; unlimited ?: boolean ; addition ?: string []; user ?: string ; bottom ?: string ; fontSize ?: string ; speed ?: number ; opacity ?: number ; callback ?: () => void ; apiBackend ?: DanmakuAPIBackend ; } export interface DPlayerSubtitleOptions { url : string ; type ?: SubTitleType ; fontSize ?: string ; bottom ?: string ; color ?: string ; backgroundColor ?: string ; offset ?: number ; } export interface DPlayerOptions { container : HTMLElement | null ; autoplay ?: boolean ; video : DPlayerVideoOptions ; danmaku ?: DPlayerDanmakuOptions ; subtitle ?: DPlayerSubtitleOptions ; }
mkv格式视频提取字幕文件(python + webvtt) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 import osimport subprocessfrom pathlib import Pathdef extract_subtitles_from_mkv (source_folder: str , target_folder: str ): Path(target_folder).mkdir(parents=True , exist_ok=True ) mkv_files = [f for f in os.listdir(source_folder) if f.endswith('.mkv' )] for mkv_file in mkv_files: source_path = os.path.join(source_folder, mkv_file) base_name = os.path.splitext(mkv_file)[0 ] command = ['ffmpeg' , '-i' , source_path] try : result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True , encoding='utf-8' ) stderr_output = result.stderr except UnicodeDecodeError as e: result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stderr_output = result.stderr.decode('gbk' ) subtitle_streams = [] for line in stderr_output.split('\n' ): if 'Subtitle:' in line: parts = line.split(',' ) idx = parts[0 ].strip().split('Stream #' )[1 ].split(':' )[0 ] subtitle_streams.append(idx) if not subtitle_streams: print (f"No subtitles found in {mkv_file} " ) continue for idx in subtitle_streams: target_path = os.path.join(target_folder, f"{base_name} _sub{idx} .vtt" ) ffmpeg_command = [ 'ffmpeg' , '-i' , source_path, '-map' , f'0:s:{idx} ' , '-f' , 'webvtt' , target_path ] print (f"Extracting subtitles from '{mkv_file} ' to '{target_path} '..." ) try : subprocess.run(ffmpeg_command, check=True ) print (f"Extracted subtitles to '{target_path} ' successfully." ) except subprocess.CalledProcessError as e: print (f"Failed to extract subtitles from '{mkv_file} ': {e} " ) def main (): source_folder = './input' target_folder = './output' extract_subtitles_from_mkv(source_folder, target_folder) if __name__ == '__main__' : main()