Easy editing, video magic, text manipulation to create a wonderful movie (cut the video with subtitles)

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.mp4and generate 11-28-18_cut.mdto 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.mdedited .

[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.mp4and 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 -vis to E:\autocutmap the folder storing video on the host to /autocut/videothe directory . E:\autocutIt 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 .srtand .mdresult.

autocut -t 22-52-00.mp4
  1. 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 are mediumand large, but GPU is recommended for better speed. Faster tinyand base, 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
  1. The default video bitrate is --bitrate 10m, you can increase or decrease it as needed.

  2. If you are not used to Markdown format files, you can also delete unnecessary sentences directly in srtthe file , and just do not pass in mdthe file name when cutting. that isautocut -c 22-52-00.mp4 22-52-00.srt

  3. If there is only srtthe file and it is inconvenient to edit, you can use the following command to generate mdthe file, and then edit mdthe file , but at this time it will be completely srtgenerated according to , and no prompt text no speechsuch .

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

some tips

  1. 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.
  2. Subtitles generated from the final video often require some minor editing. You can edit mdthe file directly ( srtmore compact than file, with embedded video). Then use autocut -s 22-52-00.md 22-52-00.srtto generate updated subtitles 22-52-00_edited.srt. Note that whether the sentence is selected or not will be ignored here, but all converted to srt.
  3. Subtitles generated from the final video often require some minor editing. But there are too many blank lines srtinside . You can use autocut -s 22-52-00.srtto generate a compact version 22-52-00_compact.srtfor 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.srtswitch back to normal formatting.
  4. 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.
  5. 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

  1. The output is garbled characters?

    AutoCut's default output encoding is utf-8. Make sure your editor also uses utf-8the encoding . You can --encodingspecify . However, it should be noted that the encoding format when generating subtitle files and using subtitle file editing needs to be consistent. For example use gbk.

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

    If other encoding formats (such as gbketc. mdto 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.

  2. 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.

  3. 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 --devicewith . For example

    autocut -t 11-28-18.mp4 --whisper-model large --device cpu
    
  4. Can it be pipinstalled ?

    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.gitthis . 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

Guess you like

Origin blog.csdn.net/ws327443752/article/details/130183567