How can I play Video or Audio on a Jupyter Notebook through VS code?

Viewed 3425

I'm running a Jupyter Notebook on VS code and trying to display/play a video. From all the other forums, I've seen that using IPython.display is the standard method; however, it isn't working for me.

For example, for Video:

from IPython.display import Video
Video('test.mp4')

This code generates a video box in the output and I don't have any errors but I can't press play. The same happens when I try playing an Audio file.

I've ensured that the file is in the current folder and I'm using Python 3.8.2 in a virtual environment (venv) and IPython 7.27.0.

2 Answers
  1. Check if there's something wrong with your .mp4 file;
  2. Try to reinstall Jupyter extension since your code works in my project: enter image description here

When it comes to use Video() to play audio file like .mp3, run code cell, there's a window like video but nothing more, no media played.

You may install the module playsound to play audio like

from playsound import playsound
playsound("song.mp3")

To get it to work I did the following:

Uninstalled and reinstalled VS Code and installed the extensions Python, Jupyter and Jupyter Keymap

Installed FFmpeg through Homebrew:

brew install ffmpeg

Converted the video codec from "MPEG4" to "H.264":

ffmpeg -i test.mp4 video.mp4

Then used the following code to display the video:

from IPython.display import Video
Video('video.mp4', width=128, height=128) 

Video output screenshot

Related