DEV Community

Kuba
Kuba

Posted on

Make Rick Astley sing in your console πŸŽ™

This is kind of post that I'm still thinking why I did that πŸ€”. But remember that not every line of code should be serious. Keep it fun! πŸŽ™

Let's make Rick Astley sing "Never Gonna Give You Up" in console.

Step first β€” Video

I assume that you already have some .mp4 file. If not, you can use pytube. Check out this snippet:

import pytube

class YT_video():
    def __init__(self, url): 
        self.url = url
        youtube = pytube.YouTube(self.url)
        self.video = youtube.streams.get_highest_resolution()

    def download(self, dest_folder = 'downloads'):
        self.video.download(dest_folder)

Enter fullscreen mode Exit fullscreen mode

Read file and display it with OpenCV

import cv2 

video = cv2.VideoCapture(video_path)

while True:
    _, frame = video.read()
    cv2.imshow("video", frame)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

video.release()
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

Ok now we have our core. We play video, but there is no sound, it is playing to fast and our goal is to have it in console.

Let's solve those problems one by one.

Sound

You can use ffpyplayer package. I added to our code next part for handling sound. It's only music player that will start song in the background.

import cv2 

video = cv2.VideoCapture(video_path)
music_player = MediaPlayer(video_path)

while True:
    _, frame = video.read()    
    cv2.imshow("video", frame)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

video.release()
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

But wait, Rick is dancing twice as fast as sings. That is not spreading joy πŸ€”.

Speed

To know FPS (Frames Per Second) we will use OpenCV. First we calculate how long should one frame last. Then we will wait a bit before displaying next one.

fps = video.get(cv2.CAP_PROP_FPS)
seconds_per_frame = 1 / fps
Enter fullscreen mode Exit fullscreen mode

Now add small wait in the end of out While.

import cv2 

video = cv2.VideoCapture(video_path)
music_player = MediaPlayer(video_path)

while True:
    frame_t_start = time.time()

    _, frame = video.read()    
    cv2.imshow("video", frame)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

    while ( time.time() - frame_t_start ) < seconds_per_frame:
        pass

video.release()
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

To Console!

Before we print it to console, we need to make our video grayscale. Use below to change frame. I've added some threshold to make it more 'binary' for console. You can play with values treshold and treshold_type. Check docs of OpenCV to read more.

I set my treshold_type for 3 and treshold around 120.


frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, frame = cv2.threshold(frame, treshold, 255, treshold_type )

Enter fullscreen mode Exit fullscreen mode

Now our image is a 2D matrix of numbers. We will replace every number with some character. Assuming that our grayscale is from 0 to 255, find a char that suits perfect.

Assume that our grayscale is such string:

GRAY_SCALE = "@$#*!=;:~-,. "
Enter fullscreen mode Exit fullscreen mode

Method for mapping number to char will be:


def grayScaleNumber(num):
    scale_size = len(GRAY_SCALE)
    index = int((num / 255) * scale_size)
    return GRAY_SCALE[index]

Enter fullscreen mode Exit fullscreen mode

Let's add simple print method instead of cv2.imshow("video",frame).

One more thing here is to resize the video. OpenCV can handle it for us.

def printFrameInConsole(frame, height, width):
    console_out_dim = ( int(width / SCALE),int(height / SCALE))
    frame = cv2.resize(frame, console_out_dim, interpolation = cv2.INTER_AREA)

    to_print = ''
    for row in frame: 
        to_print += ' '.join([ grayScaleNumber(num) for num in row.tolist()]) + "\n"

    sys.stdout.write(to_print)
    sys.stdout.flush()

Enter fullscreen mode Exit fullscreen mode

Enjoy

Remember to play with it a bit. Check out some different thresholds and threshold types. Find the best Rick Astley for yourself.

I will upload some video as soon as I can. Now admire console Rick πŸ˜…

Rick_astley

Inspired by PLED.

Top comments (1)

Collapse
 
biffbaff64 profile image
Richard Ikin

I'd rather not have Rick Astley singing in my console, if you don't mind.