笔记笔记
  • Home
  • AI&ML
  • Example
  • Zoo
  • 关于
⌘ K
目录
灵感
统一资源标识符
画图工具
收藏的正则表达式
灰度站点
LaTeX
DNS
下载 WWDC 字幕
暂存
工具
工具
服务器软件
MinIO
Shell 软件
Git
ImageMagick
MyCLI
MySQL
桌面软件
Chrome
DataGrip
IntelliJ IDEA 技巧
最后更新时间:
Copyright © 2023-2024 | Powered by dumi | GuoDapeng | 冀ICP备20004032号-1 | 冀公网安备 冀公网安备 13024002000293号

TABLE OF CONTENTS

‌
‌
‌
‌

下载 WWDC 字幕

下载 WebVTT

在视频页面打开网络,找到 *.webvtt 文件链接,使用下面命了下载所有字幕:

curl -O "https://.../sequence_[0-200].webvtt"

拖动视频到结尾附近,快速找到 *.webvtt 文件最大数字。下载范围可以比最大数字大,多出来的会下载空文件,不影响最后的结果。

把 WebVTT 字幕拼接起来:

cat *.webvtt > wwdc2024-10138_hd.webvtt

转换为 UTF-8

# 可以使用 `file` 命令查看文件编码,这里就是好奇是什么编码,就是想看看
file -I wwdc2024-10138_hd.webvtt
# wwdc2024-10138_hd.webvtt: text/plain; charset=unknown-8bit
# 转换成 utf-8;-c 忽略错误字符,这个参数很有用,避免丢失的字幕
iconv -c -t utf-8 wwdc2024-10138_hd.webvtt > wwdc2024-10138_hd_utf8.webvtt

转换为 SRT

WebVTT 转换 SRT Python 脚本:

from datetime import datetime
def parse_webvtt_timestamp(ts):
timestamp = ts.strip().split(' ')[0]
# 将 WebVTT 时间戳转换为 datetime 对象
return datetime.strptime(timestamp.replace(',', '.'), '%H:%M:%S.%f')
def format_srt_timestamp(ts):
# 将 datetime 对象格式化为 SRT 时间戳
return ts.strftime('%H:%M:%S,%f')[:-3]
def read_webvtt(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
subtitles = []
i = 0
while i < len(lines):
line = lines[i].strip()
if line == 'WEBVTT' or line.startswith('NOTE') or line.startswith('X-TIMESTAMP-MAP='): # 跳过 WEBVTT 标签和注释行
i += 1
continue
if '-->' in line and line.rstrip().endswith('WEBVTT'): # 跳过 WEBVTT 污染的时间
i += 1
continue
if '-->' in line:
timestamp_line = line.strip()
start, end = [parse_webvtt_timestamp(ts) for ts in timestamp_line.split(' --> ')]
text_lines = []
i += 1
while i < len(lines) and lines[i].strip() != '':
text_lines.append(lines[i].strip())
i += 1
text = '\n'.join(text_lines)
subtitles.append({
'start': start,
'end': end,
'text': text
})
else:
i += 1
# 去重处理
return list({(subtitle['text'], subtitle['start'], subtitle['end']): subtitle for subtitle in subtitles}.values())
def write_srt(subtitles, file_path):
# 过滤掉文本结尾是 WEBVTT 的字幕项
filtered_subtitles = [subtitle for subtitle in subtitles if not subtitle['text'].rstrip().endswith('WEBVTT')]
with open(file_path, 'w', encoding='utf-8') as file:
for i, subtitle in enumerate(filtered_subtitles, start=1):
file.write(f"{i}\n")
file.write(f"{format_srt_timestamp(subtitle['start'])} --> {format_srt_timestamp(subtitle['end'])}\n")
file.write(f"{subtitle['text']}\n\n")
def convert_webvtt_to_srt(webvtt_file_path, srt_file_path):
subtitles = read_webvtt(webvtt_file_path)
subtitles.sort(key=lambda x: x['start'])
write_srt(subtitles, srt_file_path)
# 使用示例
convert_webvtt_to_srt('wwdc2020_10652_hd_utf8.webvtt', 'wwdc2020_10652_hd.srt')

额外内容

# 将 SRT 转换为 WebVTT
ffmpeg -i wwdc2024-10138_hd.srt wwdc2024-10138_hd.vtt
# 提取视频,无音轨
ffmpeg -i wwdc2024-10136_hd.mp4 -c:v copy -an wwdc2024-10136_hd_v.mp4
# 提取音频
ffmpeg -i wwdc2024-10136_hd_zho.mp4 -q:a 0 -map a wwdc2024-10136_hd_zho.aac
# 视频、音频合并,这样可以避免压缩画质,还能更换音频
ffmpeg -i wwdc2024-10136_hd_v.mp4 -i wwdc2024-10136_hd_zho.aac -c:v copy -c:a aac wwdc2024-10136_hd_zho.mp4
# 将 MP4 转换为 HLS
ffmpeg -i ../wwdc2024-10138_hd.mp4 -c:v hevc_videotoolbox -b:v 2M -preset medium -g 30 -f hls -hls_time 1 -hls_list_size 0 output.m3u8