最近下的一个视频字幕是webVTT格式的,但是pc端没找到能支持这种格式的视频播放器,所以每次需要手动转换字幕格式,从 .vtt转换为 .srt 。 从github上找了一个,不过只能转换指定目录下的 .vtt文件,而无法递归处理子目录下的字幕文件,所以自己用golang重新写了一个命令行工具。

 

对比 VTT 与 SRT两种字幕格式:

需要处理的就两点:

1.字幕序号。vtt是从0开始,而srt是从1开始

2.字幕的时间格式。 vtt是00:02.640 ,而 srt是 00:00:02,640 。

 

具体代码如下:

github : https://github.com/ksharpdabu/WebVTTtoSRTConverter

 

package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"time"
)

func main() {
	var files []string
	rootDir := flag.String("rootDir", "", "the path of directory contains .vtt files")
	flag.Parse()

	if *rootDir == "" {
		fmt.Print(" Please input VTT directory path!")
		return
	}

	//Iterate the specify directory  and find all .vtt files
	err := filepath.Walk(*rootDir, func(path string, info os.FileInfo, err error) error {
		if strings.Contains(path, ".vtt") {
			files = append(files, path)
		}
		return nil
	})

	if err != nil {
		panic(err)
	}

	for _, file := range files {
		converterSubtitles(file)
	}

}

//converter the vtt subtitle to the srt subtitle
func converterSubtitles(path string) {
	//获取文件所在的文件夹
	dir, fileName := filepath.Split(path)

	srtFile := dir + strings.TrimSuffix(fileName, ".vtt") + ".srt"
	f, err := os.Create(srtFile)
	if err != nil {
		fmt.Println("err = ", err)
		return
	}

	defer f.Close()

	vttFile, err := os.Open(path)
	if err != nil {
		fmt.Println("err = ", err)
		return
	}

	defer vttFile.Close()

	r := bufio.NewReader(vttFile)
	lineNumber := 1
	var lineStr string
	var isCaptionLine = false
	for {
		//read file by line
		buf, _, err := r.ReadLine()

		if err != nil {
			if err == io.EOF {
				break
			}
		}

		lineStr = string(buf)
		if isTimeLine(lineStr) {

			//字幕行号
			f.WriteString(strconv.Itoa(lineNumber) + "\n")
			lineNumber++


			timeStrArr := strings.Split(strings.TrimSuffix(lineStr, "\n"), " --> ")
			const (
				vttFormatStr = "04:05.000"
				srtFormatStr = "00:04:05.000"
			)
			time1, _ := time.Parse(vttFormatStr, timeStrArr[0])
			timeSrt1 := strings.Replace(time1.Format(srtFormatStr), ".", ",", 1)
			time2, _ := time.Parse(vttFormatStr, timeStrArr[1])
			timeSrt2 := strings.Replace(time2.Format(srtFormatStr), ".", ",", 1)
			lineStr = timeSrt1 + " --> " + timeSrt2 + "\n"
			f.WriteString(lineStr)
			//下一行就是字幕
			isCaptionLine = true

		} else if isCaptionLine {
			f.WriteString(lineStr + "\n\n")
			isCaptionLine = false
		}
	}
}

/**
  判断是否是时间行
*/
func isTimeLine(line string) bool {
	return strings.Contains(line, "-->")
}

 

 

 

Convert WebVTT subtitles to SubRip

Usage:

WebVTTtoSRTConverter.exe  -rootDir  "C:\workspace\workspace_go\WebVTTtoSRTConverter\subtitle" 

Options:

-rootDir : The path of the directory contains .vtt files. The program will iterate this directory and its subdirectory.

Note: You can build other versions by yourself.

References:

 

 

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.