Automatic video editing with Python, moviepy and vosk API
You can read detailed tutorial on Towards Data Science.
See the videos:
First, you need to install moviepy and vosk libraries. You can do it with pip install -r requirements or pip install moviepy vosk command.
Next, you need to download the vosk model from official site and unzip it.
Next open automatic_video_cutter.py file with any text editor and set the parameters before calling a main method (in 305 line):
-
model_path- path to unziped vosk model; -
video_path- path to video file to convert; -
result_path- new filename to save final video; -
silence- processing method. If True, process video with silence mode, if False - with control words; -
bitrate- bitrate for write_videofile function. Default is None, must be like '2500k', '5000k', '10000k' etc. -
threshold- threshold of silence time in seconds. Used only if silence==True; -
offset_silence- offset in seconds. Used only if silence==True; -
start_word- control word that signals the beginning of the video fragment to be cut. Used only if silence==False; -
end_word- control word that signals the ending of the video fragment to be cut. Used only if silence==False; -
offset_words- offset in seconds. Used only if silence==False.
Now you can run the script using python automatic_video_cutter.py command.
If you can use Jupyter Notebook you can open automatic_video_cutter.ipynb file and specify these parameters in Set parameters 14 cell. Path to the vosk model is specified earlier - in Loading a vosk model section, 11 cell.
The project has the following structure:
automatic_video_editing
├───models
│ ├───vosk-model-en-us-0.21
│ └───vosk-model-ru-0.10
├───videos
│ .gitignore
│
│ automatic_video_cutter.ipynb
│ automatic_video_cutter.py
│ Word.py
│
│ README.md
└── requirements.txt
It contains the following folders:
- The
modelsfolder contains vosk models downloaded from official site. - The
videosfolder contains videos to be processed.
And code files:
Word.pyfile describes theWordclass.automatic_video_cutter.ipynbnotebook contains a full program.automatic_video_cutter.pyfile contains the same full program but in the form of a python script. It can be used to run a program without Jupyter Notebook.
