🐍Music video clip but every word is a Google image🎵
Tags: python, opensource, googlecloud, ai
Published: 2023-03-30 07:12:20

{% embed https://github.com/PLsergent/OWOI_AudioToClip %}

## A cool idea 💡

Everything started when I saw this video:
{% embed https://www.youtube.com/watch?v=FnWK2qG7Mas&ab_channel=VanthexOfficial %}
[Sicko Mode but every word is a Google image](https://www.youtube.com/watch?v=FnWK2qG7Mas&ab_channel=VanthexOfficial)

I just loved the idea to have an image for every word in the lyrics. A few years ago I was thinking about automating the process.

## The project 📁

In my engineering school we had to do a project for the semester and so we decided to give it a try.

The idea was to produce a website where people could upload songs of their choices and generate similar video clips with this idea: **each word becomes an image**.

Here is the repo for the front end but we won't dive into this in this article.

{% embed https://github.com/layfredrc/OneWord_OneImage %}

## The tools 🔨

In order to automate the creation of clips we would need tools to:

- Recognize the lyrics of any songs with precise timestamp of each word
- Fetch the images from Google Image
- Concatenate the images to create a video clip

### Lyrics recognition

We looked at different tools, the obvious better option was to use an API that would give us the lyrics directly (for instance [Musicxmatch](https://developer.musixmatch.com/)). But the issue was that we needed the timestamp of each word in order to match the pictures with the song.

![Google cloud](https://i.pcmag.com/imagery/reviews/02yVL9f8Jw1atwoG6sgFZDH-7..v1569482492.jpg)

That's why we decided to use [Google speech-to-text](https://cloud.google.com/speech-to-text).

This API powered by AI is originally not made to recognize voices singing, this will limit us in term of songs possibilities in the future. But with it we could get the lyrics with pretty good accuracy (again depending on the music) as well as the timestamps.

### Google images

To fetch images from Google Image we simply used the Python package [Google-Images-Search](https://pypi.org/project/Google-Images-Search/).

---

Tricky part for me:
- You'll need to create a *search engine* here: https://programmablesearchengine.google.com/
- In the package documentation they mention the `project_cx` which is actually your search engine id, which looks like this: `234903464501239304239:ccxz234er`

### Video clip editing

That's pretty much a no brainer, we used the package [MoviePy](https://zulko.github.io/moviepy/).

---

Even though you might struggle a bit to debug issues related to [Image Magick](https://www.imagemagick.org/script/index.php), the documentation is pretty well made.

This link saved me when MoviePy couldn't find Image Magick: https://github.com/Zulko/moviepy/issues/693#issuecomment-355587113

Also be careful when using subtitles, you'll need to have the selected font installed on your system.

## The dev 🧑‍💻

![Classes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1h5q4zfqp6quiv9fkps.PNG)

Without going into the details you'll find those 2 mains classes:
- **TranscriptFactory**: used to translate the song to lyrics
- **ClipMakerFactory**: used to create the video clip

---

![utils.py](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d0q3ittg45jaorb3b9q.PNG)

I eventually added a few useful functions to upload things to a GCP bucket, delete locally downloaded images and more importantly the ability to get an audio file from a YouTube link, which makes the module easier to use.

---

![Overall schema](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bz7h9l7qcoot9k0cq86d.PNG)

And finally here is the complete process to create a video:
- Use a YouTube link => extract audio file and upload to the bucket
- Extract the lyrics and timestamps with Google speech-to-text
- Iterate over the words to get images from Google Image
- Create individual temporary image clip for each word
- Concatenate the clips and add the music
- Upload video to the bucket

## Example of usage

Full code: [example usage on GitHub](https://github.com/PLsergent/OWOI_AudioToClip/blob/main/samples/basic_example_usage.py)

![code example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1vg8y2ws2nysr2o3wxq.png)

## Result 📼

{% embed https://youtu.be/h-YjnjHKo10 %}

{% embed https://youtu.be/4b-Yrsq13YA %}

The results are pretty convincing and quite funny 😄.

It's sometimes hard to see why some images have been selected. It's important to understand that the program is first looking for high resolution images and taking the first result. Also, here are some params I used:

```
_search_params = {
  "fileType": "jpg",
  "safe": "medium",
  "imgColorType": "color",
}
```

So those specific parameters sometimes produce interesting results to say the least.

There is a lot of things to take into account in order to not hurt people. The parameter `safe` is doing a good job at keeping the images "friendly" but sometimes it's more delicate. Like a few times for the word "down" the program would use a picture of someone having the down syndrome, or for the word "Turkey" it would show the recent earthquake that happened in the country.

There is a lot of political and moral aspects to consider and the program is not capable of having a critical point of view on this.

## Improvement 🚀

I'm pretty happy with the result but not perfect by any means.

The AI speech-to-text struggles a lot when it comes to something else than rap music. Which make sense considering how the artists sing. The first improvement would be to find a different way to get the lyrics and timestamps from the audio. With the recent upgrades in the AI world I wouldn't be surprised if that'd be possible soon (or if someone has the solution in the comments lol).

The processing time is very very long (about 4-5 mins for a 20-30 secs clip). The longest part is the audio recognition.

And finally as I said before, we would need a way to prevent any hurtful pictures to appear in the video clips.