Easily Make Youtube Compilations Using Python
Building Your Python Robot Fleet
Backstory
Over the past couple of years I had seen a number of compilation meme videos where they showed political figures repeating words or phrases or have contradicted themselves. On top of that there’s been instances such as YouTubers click-baiting their video where it would take me a significant amount of time to search for this reference.
Recently a Python library called Youtube-DL was taken down from Github, after quickly being restored. This brought the library to front of mind, and had me thinking of ways that I could automate things such as the previously referenced compilation videos or save me the time of manually searching for the part of a video that had me clicking on it.
After reading through this you should be able to create your own Millions and Billions Meme video or be able to find small reference to a photographer selling a photo for $6.5 million dollars in a podcast uploaded to YouTube by Peter McKinnon.
Requirements
In addition to the following libraries the environment that you are working with should have Python 3 installed, and ffmpeg.
youtube-dl — A Python library and command-line tool that allows for the downloading of YouTube videos.
videogrep — Videogrep is a command line tool that searches through dialog in video files (using .srt or .vtt subtitle tracks, or pocketsphinx transcriptions) and makes supercuts based on what it finds.) [1]
ffmpeg — This is a cross-platform library that allows for a number of functions with audio and video. (This comes pre-installed if you are working in a Google Colab.)
Create Your Own Youtube Grepper
Step 1: Install Requirements
The first step is to install both videogrep, and youtube-dl. If working in a Google Colab the output can be supressed with the %%capture instruction.
%%capture
!pip install videogrep youtube-dl
from videogrep import videogrep
Step 2: Download Video & Generate Subtitles
Once the required libraries are installed the next step is to allow for the url of a YouTube video to be provided to the youtube-dl library as the target to download. It’s easiest to use the command line interface (CLI) to achieve this download. You may notice that there are a number of parameters provided in addition to the url. In order to find the parts of the video that you want to surface you will also need to download the subtitle file as well. The follow command also downloads the automatically generated english subtitles so that they can serve as anchors for where the video will be spliced at.
video = "https://www.youtube.com/watch?v=e-L-C0Ijvfg" #@param {type:"string"}
!youtube-dl --write-auto-sub --srt-lang en $video -o video
NOTE: There are times when this step needs to be run twice in the Google Colab. I am unsure what causes this issue.
Subtitle Grepper & Video Stitcher
Once the video, and subtitles are downloaded the next step is to provide some words or phrases that we want to search the video for. The videogrep library that is able to search through the dialog in the video using the downloaded subtitles so we can use that by using the CLI provided.
search_terms = "millions|billions" #@param {type:"string"}
!videogrep -i video.mkv --use-vtt --search "$search_terms"
Bonus: Play Video In Google Colab
If working in Google Colab you may want to play your video that contains the terms within the notebook itself. We can do this using the IPython library by reading the video data into the HTML object, and enclosing it in some video controls.
from IPython.display import HTML
from base64 import b64encode
mp4 = open('supercut.mp4','rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
HTML("""
<video width=400 controls>
<source src="%s" type="video/mp4">
</video>
""" % data_url)
Otherwise if you upgrade iPython as shown below.
!pip install -U ipython
Displaying a video can be as simple as using the Video object in the IPython library.
from IPython.display import Video
Video('supercut.mp4', embed=True, width=400)
Conclusion
It’s actually pretty easy to leverage existing Python Libraries to quickly find a reference to something in a video or create your own compilation meme video. Hopefully following this article you can do the same yourself in an automated fashion. If you had trouble following allow feel free to make a copy of the Google Colab that I completed earlier. Have fun playing around with your new creation!
Google Colaboratory
Edit descriptioncolab.research.google.com
Other Python Robots
Add A New Dimension To Your Photos Using Python
Breathe New Life Into Your Photos With A 3D Ken Burns Effectmedium.com
References
- A Blog About Doing This Without the VideoGrep Library
- VideoGrep Python Library
- Youtube-DL Python Library
Enjoying these posts? Subscribe for more
Subscribe nowAlready have an account? Sign in