场景

适合如下场景:

  • 视频本身不带字幕
  • 直接看英文视频比较吃力

此时可以使用autosub这个工具来根据视频中的声音来生成对应的字幕,虽然生成的字幕并不总是那么正确,但当做参考还是不错的。

autosub简介

autosub是一款由python 2编写的通过Google Web Speech API和FFmpeg来获取视频subtitle的软件

Github地址: https://github.com/agermanidis/autosub

Github上的Usage:

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
$ autosub -h
usage: autosub [-h] [-C CONCURRENCY] [-o OUTPUT] [-F FORMAT] [-S SRC_LANGUAGE]
[-D DST_LANGUAGE] [-K API_KEY] [--list-formats]
[--list-languages]
[source_path]
positional arguments:
source_path Path to the video or audio file to subtitle
optional arguments:
-h, --help show this help message and exit
-C CONCURRENCY, --concurrency CONCURRENCY
Number of concurrent API requests to make
-o OUTPUT, --output OUTPUT
Output path for subtitles (by default, subtitles are
saved in the same directory and name as the source
path)
-F FORMAT, --format FORMAT
Destination subtitle format
-S SRC_LANGUAGE, --src-language SRC_LANGUAGE
Language spoken in source file
-D DST_LANGUAGE, --dst-language DST_LANGUAGE
Desired language for the subtitles
-K API_KEY, --api-key API_KEY
The Google Translate API key to be used. (Required for
subtitle translation)
--list-formats List all available subtitle formats
--list-languages List all available source/destination languages

Macos 下安装方法

1
2
brew install ffmpeg
pip install autosub

批量生成

原程序参数貌似不支持多文件,基于自身需要,写了个粗糙的小脚本来批量转换整个目录下的视频文件。目前够用,后续如果有子目录以及除mp4的其他格式的需求,再改进

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
USAGE="USAGE: $0 dir|file"
if [ "$1" == "" ]; then
echo $USAGE
exit 1
fi
# file process
if [ -f "$1" ]; then
echo "Start to use autosub to generate subtitle for video $1"
autosub "$1"
exit 0
fi
# dir process
if [ ! -d "$1" ]; then
echo $USAGE
exit 1
fi
base_dir="$1"
echo "**************************************************"
echo "Start to use autosub to generate subtitle for video in $base_dir"
echo "**************************************************"
echo "cd $base_dir"
echo ""
cd "$base_dir"
find . -iname "*.mp4" | while read file
do
echo "oooooooooooooooooooooooooo"
echo " ==== Start to process file - ${file}"
file_basename=`basename "$file" .mp4`
subtitle_file_name="${file_basename}.srt"
# echo "subtitle_file_name is [${subtitle_file_name}]"
if [ -e "$subtitle_file_name" ]; then
echo "Subtitle of $file has already existed. don't need to process again"
else
autosub "$file"
fi
echo " ==== End to process file - ${file}"
echo "oooooooooooooooooooooooooo"
echo ""
done
echo ""
echo "**************************************************"
echo "End to use autosub to generate subtitle for video in $base_dir"
echo "**************************************************"

留言