python 视频加字幕_subsync - 自动将字幕与视频同步

FFsubsync

badge.svg68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d6d61726f6f6e2e73766768747470733a2f2f696d672e736869656c64732e696f2f707970692f707976657273696f6e732f666673756273796e632e73766768747470733a2f2f72656164746865646f63732e6f72672f70726f6a656374732f666673756273796e632f62616467652f3f76657273696f6e3d6c617465737468747470733a2f2f696d672e736869656c64732e696f2f707970692f762f666673756273796e632e737667

Language-agnostic automatic synchronization of subtitles with video, so that subtitles are aligned to the correct starting point within the video.

Turn this:

Into this:

tearing-me-apart-wrong.gif

tearing-me-apart-correct.gif

Helping Development

At the request of some, you can now help cover my coffee expenses using the Github Sponsors button at the top (recurring monthly payments), or using the below Paypal Donate button (one-time payment):

68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f4c472e676966

Install

First, make sure ffmpeg is installed. On MacOS, this looks like:

brew install ffmpeg

Next, grab the script. It should work with both Python 2 and Python 3:

pip install ffsubsync

If you want to live dangerously, you can grab the latest version as follows:

pip install git+https://github.com/smacke/ffsubsync@latest

Usage

ffs, subsync and ffsubsync all work as entrypoints:

ffs video.mp4 -i unsynchronized.srt -o synchronized.srt

There may be occasions where you have a correctly synchronized srt file in a language you are unfamiliar with, as well as an unsynchronized srt file in your native language. In this case, you can use the correctly synchronized srt file directly as a reference for synchronization, instead of using the video as the reference:

ffsubsync reference.srt -i unsynchronized.srt -o synchronized.srt

ffsubsync uses the file extension to decide whether to perform voice activity detection on the audio or to directly extract speech from an srt file.

Speed

ffsubsync usually finishes in 20 to 30 seconds, depending on the length of the video. The most expensive step is actually extraction of raw audio. If you already have a correctly synchronized "reference" srt file (in which case audio extraction can be skipped), ffsubsync typically runs in less than a second.

How It Works

The synchronization algorithm operates in 3 steps:

Discretize video and subtitles by time into 10ms windows.

For each 10ms window, determine whether that window contains speech. This is trivial to do for subtitles (we just determine whether any subtitle is "on" during each time window); for video, use an off-the-shelf voice activity detector (VAD) like the one built into webrtc.

Now we have two binary strings: one for the subtitles, and one for the video. Try to align these strings by matching 0's with 0's and 1's with 1's. We score these alignments as (# video 1's matched w/ subtitle 1's) - (# video 1's matched with subtitle 0's).

The best-scoring alignment from step 3 determines how to offset the subtitles in time so that they are properly synced with the video. Because the binary strings are fairly long (millions of digits for video longer than an hour), the naive O(n^2) strategy for scoring all alignments is unacceptable. Instead, we use the fact that "scoring all alignments" is a convolution operation and can be implemented with the Fast Fourier Transform (FFT), bringing the complexity down to O(n log n).

Limitations

In most cases, inconsistencies between video and subtitles occur when starting or ending segments present in video are not present in subtitles, or vice versa. This can occur, for example, when a TV episode recap in the subtitles was pruned from video. FFsubsync typically works well in these cases, and in my experience this covers >95% of use cases. Handling breaks and splits outside of the beginning and ending segments is left to future work (see below).

Future Work

Besides general stability and usability improvements, one line of work aims to extend the synchronization algorithm to handle splits / breaks in the middle of video not present in subtitles (or vice versa). Developing a robust solution will take some time (assuming one is possible). See #10 for more details.

History

The implementation for this project was started during HackIllinois 2019, for which it received an Honorable Mention (ranked in the top 5 projects, excluding projects that won company-specific prizes).

Credits

This project would not be possible without the following libraries:

ffmpeg and the ffmpeg-python wrapper, for extracting raw audio from video

VAD from webrtc and the py-webrtcvad wrapper, for speech detection

srt for operating on SRT files

numpy and, indirectly, FFTPACK, which powers the FFT-based algorithm for fast scoring of alignments between subtitles (or subtitles and video)

Other excellent Python libraries like argparse and tqdm, not related to the core functionality, but which enable much better experiences for developers and users.

License

Code in this project is MIT licensed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用Python中的moviepy库来实现视频字幕的功能。下面是一个简单的示例代码: ```python from moviepy.editor import * # 视频字幕文件 video = VideoFileClip("video.mp4") subtitle = SubtitlesClip("subtitle.srt") # 将字幕文件和视频文件合并 result = CompositeVideoClip([video, subtitle.set_pos(('center', 'bottom'))]) # 保存结果视频 result.write_videofile("result.mp4") ``` 在上面的代码中,我们首先使用VideoFileClip函数视频文件,并使用SubtitlesClip函数字幕文件。然后,我们使用CompositeVideoClip函数将字幕文件和视频文件合并,并使用set_pos函数将字幕放在视频底部中央。最后,我们使用write_videofile函数保存结果视频。 需要注意的是,字幕文件需要符合SRT格式。如果你的字幕文件不是SRT格式,你可以使用Python中的pysrt库将其转换为SRT格式。 ### 回答2: 要在Python中给视频字幕,可以使用`moviepy`库来实现。首先需要安装`moviepy`库,可以使用`pip install moviepy`命令来进行安装。 接下来的步骤如下: 1. 导入`moviepy`库以及其相关的模块。 ``` from moviepy.editor import * ``` 2. 视频文件。 ``` video = VideoFileClip("input.mp4") ``` 3. 创建文本字幕对象,并设置字幕内容、字体、字号等相关属性。 ``` text = TextClip("Hello, World!", fontsize=70, color='white', font="Arial-Bold") ``` 4. 将字幕对象与视频对象进行合并。 ``` subtitles = CompositeVideoClip([video, text.set_position(('center', 'bottom')).set_duration(video.duration)]) ``` 5. 输出带有字幕视频文件。 ``` subtitles.write_videofile("output.mp4") ``` 以上就是使用Python视频字幕的简单步骤。你可以根据自己的需求修改字幕内容、字体等参数,实现更个性化的效果。 ### 回答3: 要在Python中为视频字幕,你可以使用一些库和工具来实现。 首先,你可以使用`moviepy`库来处理和编辑视频。这个库支持多种视频格式和功能,包括添字幕。你需要先在Python中安装这个库,然后使用`TextClip`类创建一个包含字幕文字的文本剪辑。 首先,导入`moviepy.editor`和`moviepy.video.tools.drawing`模块。创建一个视频剪辑对象,视频文件: ```python from moviepy.editor import VideoFileClip from moviepy.video.tools.drawing import TextClip # 视频文件 video = VideoFileClip("video.mp4") ``` 然后,定义一个函数来创建字幕文本剪辑。设置字幕的内容、字体、字号、颜色等属性。将字幕文本剪辑与视频剪辑合并,并设置字幕剪辑的位置和持续时间。 ```python def create_subtitle(text, font, fontsize, color): # 创建字幕文本剪辑 subtitle = TextClip(text, font=font, fontsize=fontsize, color=color) # 设置字幕文本剪辑的位置和持续时间 subtitle = subtitle.set_position(('center', 'bottom')).set_duration(video.duration) return subtitle # 创建字幕 subtitle = create_subtitle("字幕内容", 'Arial', 30, 'white') # 将字幕文本剪辑与视频剪辑合并 video_with_subtitle = CompositeVideoClip([video, subtitle]) ``` 最后,保存带有字幕视频剪辑。 ```python # 保存带有字幕视频剪辑 video_with_subtitle.write_videofile("video_with_subtitle.mp4") ``` 这就是使用Python视频字幕的基本过程。你可以根据需要自定义字幕的样式和效果,如设置背景、阴影、透明度等。希望以上内容对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值