4 Star 20 Fork 2

Gitee 极速下载 / AutoCut

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/mli/autocut
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

AutoCut: 通过字幕来剪切视频

AutoCut 对你的视频自动生成字幕。然后你选择需要保留的句子,AutoCut 将对你视频中对应的片段裁切并保存。你无需使用视频编辑软件,只需要编辑文本文件即可完成剪切。

2024.03.10更新:支持 pip 安装和提供 import 转录相关的功能

# Install
pip install autocut-sub
from autocut import Transcribe, load_audio

2023.10.14更新:支持 faster-whisper 和指定依赖(但由于 Action 限制暂时移除了 faster-whisper 的测试运行)

# for whisper only
pip install .

# for whisper and faster-whisper
pip install '.[faster]'

# for whisper and openai-whisper
pip install '.[openai]'

# for all
pip install '.[all]'
# using faster-whisper
autocut -t xxx --whisper-mode=faster
# using openai api
export OPENAI_API_KEY=sk-xxx
autocut -t xxx --whisper-mode=openai --openai-rpm=3

2023.8.13更新:支持调用 Openai Whisper API

export OPENAI_API_KEY=sk-xxx
autocut -t xxx --whisper-mode=openai --openai-rpm=3

使用例子

假如你录制的视频放在 2022-11-04/ 这个文件夹里。那么运行

autocut -d 2022-11-04

提示:如果你使用 OBS 录屏,可以在 设置->高级->录像->文件名格式 中将空格改成 /,即 %CCYY-%MM-%DD/%hh-%mm-%ss。那么视频文件将放在日期命名的文件夹里。

AutoCut 将持续对这个文件夹里视频进行字幕抽取和剪切。例如,你刚完成一个视频录制,保存在 11-28-18.mp4。AutoCut 将生成 11-28-18.md。你在里面选择需要保留的句子后,AutoCut 将剪切出 11-28-18_cut.mp4,并生成 11-28-18_cut.md 来预览结果。

你可以使用任何的 Markdown 编辑器。例如我常用 VS Code 和 Typora。下图是通过 Typora 来对 11-28-18.md 编辑。

全部完成后在 autocut.md 里选择需要拼接的视频后,AutoCut 将输出 autocut_merged.mp4 和对应的字幕文件。

安装

首先安装 Python 包

pip install git+https://github.com/mli/autocut.git

本地安装测试

git clone https://github.com/mli/autocut
cd autocut
pip install .

上面将安装 pytorch。如果你需要 GPU 运行,且默认安装的版本不匹配的话,你可以先安装 Pytorch。如果安装 Whipser 出现问题,请参考官方文档

另外需要安装 ffmpeg

# on Ubuntu or Debian
sudo apt update && sudo apt install ffmpeg

# on Arch Linux
sudo pacman -S ffmpeg

# on MacOS using Homebrew (https://brew.sh/)
brew install ffmpeg

# on Windows using Scoop (https://scoop.sh/)
scoop install ffmpeg

Docker 安装

首先将项目克隆到本地。

git clone https://github.com/mli/autocut.git

安装 CPU 版本

进入项目根目录,然后构建 docker 映像。

docker build -t autocut .

运行下面的命令创建 docker 容器,就可以直接使用了。

docker run -it --rm -v E:\autocut:/autocut/video autocut /bin/bash

其中 -v 是将主机存放视频的文件夹 E:\autocut 映射到虚拟机的 /autocut/video 目录。E:\autocut 是主机存放视频的目录,需修改为自己主机存放视频的目录。

安装 GPU 版本

使用 GPU 加速需要主机有 Nvidia 的显卡并安装好相应驱动。然后在项目根目录,执行下面的命令构建 docker 映像。

docker build -f ./Dockerfile.cuda -t autocut-gpu .

使用 GPU 加速时,运行 docker 容器需添加参数 --gpus all

docker run --gpus all -it --rm -v E:\autocut:/autocut/video autocut-gpu

更多使用选项

转录某个视频生成 .srt.md 结果。

autocut -t 22-52-00.mp4
  1. 如果对转录质量不满意,可以使用更大的模型,例如

    autocut -t 22-52-00.mp4 --whisper-model large

    默认是 small。更好的模型是 mediumlarge,但推荐使用 GPU 获得更好的速度。也可以使用更快的 tinybase,但转录质量会下降。

剪切某个视频

autocut -c 22-52-00.mp4 22-52-00.srt 22-52-00.md
  1. 默认视频比特率是 --bitrate 10m,你可以根据需要调大调小。

  2. 如果不习惯 Markdown 格式文件,你也可以直接在 srt 文件里删除不要的句子,在剪切时不传入 md 文件名即可。就是 autocut -c 22-52-00.mp4 22-52-00.srt

  3. 如果仅有 srt 文件,编辑不方便可以使用如下命令生成 md 文件,然后编辑 md 文件即可,但此时会完全对照 srt 生成,不会出现 no speech 等提示文本。

    autocut -m test.srt test.mp4
    autocut -m test.mp4 test.srt # 支持视频和字幕乱序传入
    autocut -m test.srt # 也可以只传入字幕文件

一些小提示

  1. 讲得流利的视频的转录质量会高一些,这因为是 Whisper 训练数据分布的缘故。对一个视频,你可以先粗选一下句子,然后在剪出来的视频上再剪一次。
  2. 最终视频生成的字幕通常还需要做一些小编辑。但 srt 里面空行太多。你可以使用 autocut -s 22-52-00.srt 来生成一个紧凑些的版本 22-52-00_compact.srt 方便编辑(这个格式不合法,但编辑器,例如 VS Code,还是会进行语法高亮)。编辑完成后,autocut -s 22-52-00_compact.srt 转回正常格式。
  3. 用 Typora 和 VS Code 编辑 Markdown 都很方便。他们都有对应的快捷键 mark 一行或者多行。但 VS Code 视频预览似乎有点问题。
  4. 视频是通过 ffmpeg 导出。在 Apple M1 芯片上它用不了 GPU,导致导出速度不如专业视频软件。

常见问题

  1. 输出的是乱码?

    AutoCut 默认输出编码是 utf-8. 确保你的编辑器也使用了 utf-8 解码。你可以通过 --encoding 指定其他编码格式。但是需要注意生成字幕文件和使用字幕文件剪辑时的编码格式需要一致。例如使用 gbk

    autocut -t test.mp4 --encoding=gbk
    autocut -c test.mp4 test.srt test.md --encoding=gbk

    如果使用了其他编码格式(如 gbk 等)生成 md 文件并用 Typora 打开后,该文件可能会被 Typora 自动转码为其他编码格式,此时再通过生成时指定的编码格式进行剪辑时可能会出现编码不支持等报错。因此可以在使用 Typora 编辑后再通过 VSCode 等修改到你需要的编码格式进行保存后再使用剪辑功能。

  2. 如何使用 GPU 来转录?

    当你有 Nvidia GPU,而且安装了对应版本的 PyTorch 的时候,转录是在 GPU 上进行。你可以通过命令来查看当前是不是支持 GPU。

    python -c "import torch; print(torch.cuda.is_available())"

    否则你可以在安装 AutoCut 前手动安装对应的 GPU 版本 PyTorch。

  3. 使用 GPU 时报错显存不够。

    whisper 的大模型需要一定的 GPU 显存。如果你的显存不够,你可以用小一点的模型,例如 small。如果你仍然想用大模型,可以通过 --device 来强制使用 CPU。例如

    autocut -t 11-28-18.mp4 --whisper-model large --device cpu
  4. 能不能使用 pip 安装?

    whisper已经发布到PyPI了,可以直接用pip install openai-whisper安装。

    https://github.com/openai/whisper#setup

    https://pypi.org/project/openai-whisper/

如何参与贡献

这里有一些想做的 feature,欢迎贡献。

代码结构

autocut
│  .gitignore
│  LICENSE
│  README.md # 一般新增或修改需要让使用者知道就需要对应更新 README.md 内容
│  setup.py

└─autocut # 核心代码位于 autocut 文件夹中,新增功能的实现也一般在这里面进行修改或新增
   │  cut.py
   │  daemon.py
   │  main.py
   │  transcribe.py
   │  utils.py
   └─ __init__.py

安装依赖

开始安装这个项目的需要的依赖之前,建议先了解一下 Anaconda 或者 venv 的虚拟环境使用,推荐使用虚拟环境来搭建该项目的开发环境。 具体安装方式为在你搭建搭建的虚拟环境之中按照上方安装步骤进行安装。

为什么推荐使用虚拟环境开发?

一方面是保证各种不同的开发环境之间互相不污染。

更重要的是在于这个项目实际上是一个 Python Package,所以在你安装之后 AutoCut 的代码实际也会变成你的环境依赖。 因此在你更新代码之后,你需要让将新代码重新安装到环境中,然后才能调用到新的代码。

开发

  1. 代码风格目前遵循 PEP-8,可以使用相关的自动格式化软件完成。
  2. utils.py 主要是全局共用的一些工具方法。
  3. transcribe.py 是调用模型生成srtmd的部分。
  4. cut.py 提供根据标记后mdsrt进行视频剪切合并的功能。
  5. daemon.py 提供的是监听文件夹生成字幕和剪切视频的功能。
  6. main.py 声明命令行参数,根据输入参数调用对应功能。

开发过程中请尽量保证修改在正确的地方,以及合理地复用代码, 同时工具函数请尽可能放在utils.py中。 代码格式目前是遵循 PEP-8,变量命名尽量语义化即可。

在开发完成之后,最重要的一点是需要进行测试,请保证提交之前对所有与你修改直接相关的部分以及你修改会影响到的部分都进行了测试,并保证功能的正常。 目前使用 GitHub Actions CI, Lint 使用 black 提交前请运行 black

提交

  1. commit 信息用英文描述清楚你做了哪些修改即可,小写字母开头。
  2. 最好可以保证一次的 commit 涉及的修改比较小,可以简短地描述清楚,这样也方便之后有修改时的查找。
  3. PR 的时候 title 简述有哪些修改, contents 可以具体写下修改内容。
  4. run test pip install pytest then pytest test
  5. run lint pip install black then black .
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

AutoCut 是一款通过字幕来剪切视频的工具 展开 收起
Python 等 2 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/mirrors/AutoCut.git
git@gitee.com:mirrors/AutoCut.git
mirrors
AutoCut
AutoCut
main

搜索帮助