I have found only ways to play '.mp4' files. I even tried to download .avi file and it is showing file is corrupted. I even tried to change .avi to .mp4 using online tools but nothing worked fine.
Use the python moviepy package
from moviepy.editor import *
path="file.avi"
clip=VideoFileClip(path)
clip.ipython_display(width=280)
You need to convert avi to mp4 with ffmepg
!ffmpeg -i input.avi output.mp4
Then get the file content into data_url
from IPython.display import HTML
from base64 import b64encode
mp4 = open('output.mp4','rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
Then display it with HTML()
HTML("""
<video controls>
<source src="%s" type="video/mp4">
</video>
""" % data_url)
Here's a working example.
Related
When I call imghdr.what(sampleImage.png) it recognizes it as a jpeg for some reason. For example if I use print(imghdr.what(sampleImage.png)) it will output the string "jpeg". I checked the image file and it is displayed as having the expected PNG file format on Mac OSX.
I want to play sound through my python script:
I have used win_sound
pyglet
playsound
and pygame but it doesn't want to work.
Is there any way to do this.
the directory is C:Users/Random/Folder/OtherFolder/Sound1
You can use librosa for loading audio data and sounddevice for playing audio.
Like this:
import time
import librosa
import sounddevice as sd
def play_audio(audio_path, sampling_rate=44100):
audio, sampling_rate = librosa.load(audio_path, sr=sampling_rate)
duration = librosa.core.get_duration(audio, sr=sampling_rate)
# play the audio
sd.play(audio, sampling_rate)
# wait until the audio is done playing
time.sleep(duration)
play_audio('./test.mp3')
Moreover, if this function causes the rest of your program to freeze ( because of time.sleep ) you need to run the play_audio function on a separate thread like here.
Try using the playsound module.
You need to install it first (enter in command prompt):
pip install playsound
And then you can use it as shown:
from playsound import playsound
playsound("C:Users\Random\Folder\OtherFolder\Sound1.wav")
(Replace the ".wav" thing with whatever file extension you have it set to).
I am trying to concatenate two clips using MoviePy [ Windows 10 , Python 3.7.4 ] , but there is no audio in the output video. I can see the temporary audio file while the videos are being concatenated.
from moviepy.editor import VideoFileClip, concatenate_videoclips
clip1 = VideoFileClip("C1.mp4")
clip2 = VideoFileClip("C2.mp4")
final_clip = concatenate_videoclips([clip1,clip2])
final_clip.write_videofile("my_concatenation.mp4")
Terminal gives this ouput,
Moviepy - Building video my_concatenation.mp4.
MoviePy - Writing audio in %s
MoviePy - Done.
Moviepy - Writing video my_concatenation.mp4
Moviepy - Done !
Moviepy - video ready my_concatenation.mp4
I have also tried this answer but it doesn't solve the issue. Any ideas why this might be happening?
Update MoviePy to v1.0.2 or greater, or apply the changes from https://github.com/Zulko/moviepy/pull/968 to your installation.
The issue is caused by a ffmpeg parameter , just go to moviepy -> video -> io -> ffmpeg_wrtier.py. Then search for ['-i', '-', '-an']. Then change the order into ['-an','-i','-']. Now the audio will work in any player. The first order binds the -an flag to the next stream, which is the audio file (which is subsequently ignored).
I am having some difficulties with manipulating multiple files in a Colaboratory Notebook downloaded to the /content directory in my google drive. So far, I have successfully downloaded and extracted a kaggle dataset to a Colaboratory Notebook using the following code:
!kaggle datasets download -d iarunava/cell-images-for-detecting-malaria -p /content
!unzip \cell-images-for-detecting-malaria.zip
I was also able to use Pillow to import a single file from the dataset into my Colaboratory session (I obtained the filename from the output produced during the extraction):
from PIL import Image
img = Image.open('cell_images/Uninfected/C96P57ThinF_IMG_20150824_105445_cell_139.png')
How can I access multiple extracted files from /content without knowing their names in advance?
Thank you!
After some further experimentation, I found that the python os module works similarly in Colab Notebooks as it does on an individual computer. For example, in a Colab Notebook the command
os.getcwd()
returns '/content' as an output.
Also, the command os.listdir() returns the names of all the files I downloaded and extracted.
You can use glob. glob.glob(pattern) will match all files that match the pattern. For example the code bellow will read all the .png files in the image_dir.
png = glob.glob(os.path.join(img_dir, '*.png'))
png = np.array(png)
png will contain a list of filenames.
In your case you can use:
png = glob.glob('cell_images/Uninfected/*.png')
png = np.array(png)
I made a timer in a Python script using an IDE (its working fine). When the timer ends, I want to open a sound file, like a .mp3 file, so that the user knows that the timer went off. I know I could use google for a timer, but knowing how to do this would help for later projects. Thanks!
First install pygame via pip. Then use:
from pygame import mixer # Load the required library
mixer.init()
mixer.music.load('e:/LOCAL/Betrayer/. Metalik Klinik1-Anak Sekolah.mp3')
mixer.music.play()
if you're on linux you can use pyglet! pure python:
import pyglet
music = pyglet.resource.media('music.mp3')
music.play()
pyglet.app.run()
if you're on windows its built in:
import winsound
winsound.PlaySound('sound.wav', winsound.SND_FILENAME)
EDIT
testing with winsound seemed to return nothing substantial so i found a work around using "playsound":
pip3 install playsound
in code:
import playsound
playsound.playsound("C:\\path\\to\\audio\\file.ext")
tested this with mp3 and wav and both worked just fine.
Let me know if this helped!