AutoCut: Cut video with subtitles
AutoCut automatically generates subtitles for your videos. Then you choose the sentence you want to keep, and AutoCut will cut and save the corresponding segment in your video. You don't need to use video editing software, just edit the text file to complete the cutting.
Example of use
Suppose your recorded video is placed in 2022-11-04/
this folder. then run
autocut -d 2022-11-04
Tip: If you use OBS to record the screen, you can change the space
设置->高级->录像->文件名格式
in to/
, ie%CCYY-%MM-%DD/%hh-%mm-%ss
. Then the video files will be placed in the folder named by date.
AutoCut will continue to extract and cut subtitles from videos in this folder. For example, you just finished a video recording, save it in 11-28-18.mp4
. AutoCut will generate 11-28-18.md
. After you select the sentence you want to keep, AutoCut will cut it out 11-28-18_cut.mp4
and generate 11-28-18_cut.md
to preview the result.
You can use any Markdown editor. For example, I often use VS Code and Typora. The picture below is 11-28-18.md
edited .
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-UmUqdopq-1681631536330)(imgs/typora.jpg)]
After all is done, select the video to be spliced autocut.md
in , and AutoCut will output autocut_merged.mp4
and the corresponding subtitle file.
Install
First install the Python package
pip install git+https://github.com/mli/autocut.git
local installation test
git clone https://github.com/mli/autocut
cd autocut
pip install .
The above will install pytorch . If you need GPU to run, and the default installed version does not match, you can install Pytorch first. If you have problems installing Whipser, please refer to the official documentation .
In addition, you need to install 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 installation
First clone the project locally.
git clone https://github.com/mli/autocut.git
Install CPU version
Go into the project root directory and build the docker image.
docker build -t autocut .
Run the following command to create a docker container and use it directly.
docker run -it --rm -v E:\autocut:/autocut/video autocut /bin/bash
Among them -v
is to E:\autocut
map the folder storing video on the host to /autocut/video
the directory . E:\autocut
It is the directory where the host stores videos. It needs to be changed to the directory where the host stores videos.
Install the GPU version
Using GPU acceleration requires the host to have an Nvidia graphics card and install the corresponding driver. Then in the project root directory, execute the following command to build the docker image.
docker build -f ./Dockerfile.cuda -t autocut-gpu .
When using GPU acceleration, parameters need to be added to run the docker container --gpus all
.
docker run --gpus all -it --rm -v E:\autocut:/autocut/video autocut-gpu
More usage options
Transcribe a video to generate .srt
and .md
result.
autocut -t 22-52-00.mp4
-
If you are not satisfied with the quality of the transcription, you can use a larger model, e.g.
autocut -t 22-52-00.mp4 --whisper-model large
The default is
small
. Better models aremedium
andlarge
, but GPU is recommended for better speed. Fastertiny
andbase
, but the quality of the transcription will be reduced.
cut a video
autocut -c 22-52-00.mp4 22-52-00.srt 22-52-00.md
-
The default video bitrate is
--bitrate 10m
, you can increase or decrease it as needed. -
If you are not used to Markdown format files, you can also delete unnecessary sentences directly in
srt
the file , and just do not pass inmd
the file name when cutting. that isautocut -c 22-52-00.mp4 22-52-00.srt
-
If there is only
srt
the file and it is inconvenient to edit, you can use the following command to generatemd
the file, and then editmd
the file , but at this time it will be completelysrt
generated according to , and no prompt textno speech
such .autocut -m test.srt test.mp4 autocut -m test.mp4 test.srt # 支持视频和字幕乱序传入 autocut -m test.srt # 也可以只传入字幕文件
some tips
- Transcriptions of fluent videos will be of higher quality because of Whisper's training data distribution. For a video, you can roughly select the sentence first, and then cut it again on the cut video.
Subtitles generated from the final video often require some minor editing. You can editmd
the file directly (srt
more compact than file, with embedded video). Then useautocut -s 22-52-00.md 22-52-00.srt
to generate updated subtitles22-52-00_edited.srt
. Note that whether the sentence is selected or not will be ignored here, but all converted tosrt
.- Subtitles generated from the final video often require some minor editing. But there are too many blank lines
srt
inside . You can useautocut -s 22-52-00.srt
to generate a compact version22-52-00_compact.srt
for easy editing (this format is not legal, but editors, such as VS Code, will still perform syntax highlighting). When editing is complete,autocut -s 22-52-00_compact.srt
switch back to normal formatting. - It is very convenient to edit Markdown with Typora and VS Code. They all have corresponding shortcut keys to mark one or more lines. But there seems to be something wrong with the VS Code video preview.
- Video is exported via ffmpeg. On the Apple M1 chip it can't use the GPU, resulting in slower export than professional video software.
common problem
-
The output is garbled characters?
AutoCut's default output encoding is
utf-8
. Make sure your editor also usesutf-8
the encoding . You can--encoding
specify . However, it should be noted that the encoding format when generating subtitle files and using subtitle file editing needs to be consistent. For example usegbk
.autocut -t test.mp4 --encoding=gbk autocut -c test.mp4 test.srt test.md --encoding=gbk
If other encoding formats (such as
gbk
etc.md
to generate a file and opened with Typora, the file may be automatically transcoded to other encoding formats by Typora. At this time, when editing with the encoding format specified at the time of generation, the encoding may not be supported. Wait for an error. Therefore, after editing with Typora, you can modify it to the encoding format you need through VSCode, save it, and then use the editing function. -
How to use GPU to transcribe?
When you have an Nvidia GPU and the corresponding version of PyTorch is installed, the transcription happens on the GPU. You can use the command to check whether GPU is currently supported.
python -c "import torch; print(torch.cuda.is_available())"
Otherwise, you can manually install the corresponding GPU version PyTorch before installing AutoCut.
-
When using the GPU, an error is reported that the video memory is not enough.
The large model of whisper requires a certain amount of GPU memory. If you don't have enough video memory, you can use a smaller model, eg
small
. If you still want to use large models, you can force CPU usage--device
with . For exampleautocut -t 11-28-18.mp4 --whisper-model large --device cpu
-
Can it be
pip
installed ?Because AutoCut's dependency whisper has not been released on PyPI, so it can only be published in
pip install git+https://github.com/mli/autocut.git
this . Students who need it can check whether the whisper model can be downloaded directly from the huggingface hub, so as to get rid of the dependence of the whisper package.
Project address: https://github.com/mli/autocut/blob/main/README.md