subtitle.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <a-input v-model:value="subtitleVal" allowClear placeholder="请输入" />
  3. <div style="height: 900px; overflow-y: auto" class="mt-2">
  4. <div v-if="subtitle">{{ subtitle }}</div>
  5. <div v-for="(item, idx) in subtitleList" :key="idx">
  6. <div>{{ item.startTimeStr }}~{{ item.endTimeStr }}</div>
  7. <div style="cursor: pointer; padding: 10px 0" @click="videoSpeed(item)">{{ item.text }}</div>
  8. </div>
  9. </div>
  10. </template>
  11. <script setup>
  12. import axios from 'axios'
  13. const props = defineProps({
  14. url: {
  15. type: String,
  16. default: ''
  17. }
  18. })
  19. watch(
  20. () => props.url,
  21. (newVal, oldVal) => {
  22. if (newVal) {
  23. nextTick(() => {
  24. GetSrtInfo(newVal)
  25. })
  26. }
  27. },
  28. { immediate: true }
  29. )
  30. const emit = defineEmits({ videoSpeed: null })
  31. const videoSpeed = (e) => {
  32. emit('videoSpeed', e)
  33. }
  34. // 模糊查询函数
  35. const subtitleVal = ref()
  36. const srtInfoList = ref([])
  37. const subtitleList = computed(() => {
  38. return srtInfoList.value.filter((item) => {
  39. if (subtitleVal.value) {
  40. return item.text.includes(subtitleVal.value)
  41. } else {
  42. return true
  43. }
  44. })
  45. })
  46. function getFileExtension(filename) {
  47. return filename.substring(filename.lastIndexOf('.') + 1).toLowerCase()
  48. }
  49. //获取字幕内容,发送一个get请求
  50. const subtitle = ref()
  51. const GetSrtInfo = (srtUrl) => {
  52. subtitle.value = ''
  53. if(getFileExtension(srtUrl) != 'srt'){
  54. subtitle.value = '字幕解析失败'
  55. return
  56. }
  57. axios
  58. .get(`${srtUrl}`)
  59. .then(function (response) {
  60. try {
  61. let textList = response.data
  62. .split(/\n\s\n/)
  63. .filter((item) => item != '')
  64. .map((item, index) => {
  65. let textItem = item.split(/\n/)
  66. return {
  67. index: index,
  68. sort: textItem[0],
  69. text: textItem[2],
  70. startTime: ToSeconds(textItem[1].split(' --> ')[0]),
  71. endTime: ToSeconds(textItem[1].split(' --> ')[1]),
  72. startTimeStr: toStrTime(textItem[1].split(' --> ')[0]),
  73. endTimeStr: toStrTime(textItem[1].split(' --> ')[1]),
  74. timeLine: textItem[1]
  75. }
  76. })
  77. subtitle.value = false
  78. srtInfoList.value = textList
  79. } catch (error) {
  80. subtitle.value = '字幕解析失败'
  81. }
  82. })
  83. .catch(function (error) {
  84. console.log(error)
  85. })
  86. }
  87. function toStrTime(t){
  88. return t.split(',')[0]
  89. }
  90. //将时间转化为秒
  91. function ToSeconds(t) {
  92. var s = 0.0
  93. if (t) {
  94. var p = t.split(':')
  95. for (let i = 0; i < p.length; i++) {
  96. s = s * 60 + parseFloat(p[i].replace(',', '.'))
  97. }
  98. }
  99. return s
  100. }
  101. defineExpose({
  102. GetSrtInfo
  103. })
  104. </script>
  105. <style scoped lang="less"></style>