Downloading a Python audio file Jupyter Notebook - python

I'm trying to upload an audio file to Jupyter Notebook(I work with librosa), I wrote the following code:
import librosa
import librosa.display
import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
import IPython.display as ipd
%matplotlib inline
import warnings
path = 'gruesome.mp3'
audio, sr = librosa.load(path, sr=44100)
I get the following error(tell me what the problem may be, I've already spent a lot of time, I can't solve the problem, how to solve the problem?):
enter image description here
enter image description here
enter image description here

This is well covered in librosa readme.
In summary
libsndfile doesn't support MP3 due to patents.
audioread requires that you install ffmpeg or GStreamer.
For conda installations, they recommend
conda install -c conda-forge ffmpeg
You can also consider a different file format, e.g. FLAC or ogg
Edit: I should also mention that this has nothing to do with Jupyter, nor with uploading or downloading anything.

Related

MoviePy RuntimeError: imageio.ffmpeg.download() has been deprecated

I try to run a simple script in Google Colab to determine a length of a video:
!pip install moviepy
from moviepy.editor import *
# Change directory:
os.chdir(r'/content/my_data')
clip = VideoFileClip("my_video.mp4")
print(clip.duration)
However, I obtain an error:
imageio.ffmpeg.download() has been deprecated. Use 'pip install imageio-ffmpeg' instead.'
After quick search I found that imageio needs to be downgraded by doing this:
!pip install imageio==2.4.1
So I changed the scrip to following:
!pip install moviepy
!pip install imageio==2.4.1
from moviepy.editor import *
# Change directory:
os.chdir(r'/content/my_data')
clip = VideoFileClip("my_video.mp4")
print(clip.duration)
I am still getting the same error.
Any idea how to fix it?
Thank you.
As moviepy is not in great shape nowadays I would recommend just using imageio's V2 API for the time being. It is really easy to get the metadata from a video file using it. Basically,
import imageio
path = "path/to/videofile.mp4"
video = imageio.get_reader(path)
print(video._meta)
Using the meta attribute you can access the number of frames, codec, size, etc. If you only want to get the duration (in seconds) you can just simply call for:
print(video._meta["duration"])
#Mr K.'s answer is correct; however, in modern versions of ImageIO you can use this alternative, which may be simpler:
import imageio.v3 as iio
location = "path/to/videofile.mp4"
duration = iio.immeta(location)["duration"]
print(duration)
(immeta reads the metadata of an image or video.)

PortAudioError: Error querying device -1 in google colab when using the sounddevice library

I've installed and imported all the required libraries (at least I think so):
!sudo apt-get install libportaudio2
!pip install sounddevice
import numpy as np
import matplotlib.pyplot as plt
import sounddevice as sd
And I am getting the following error when running this code:
wav_wave = np.array(tone, dtype=np.int16)
sd.play(wav_wave, blocking=True)
sd.stop()
The error below is caused by the sd.play function.
PortAudioError: Error querying device -1
You are using a virtual machine from Google collab, you can´t play an audio there. Instead, you can always display an audio object in the front-end and play it from there. Like this:
from IPython.display import Audio
from IPython.display import display
wn = Audio("saxriff.wav", autoplay=True)
display(wn)
Please note that this solution only works for notebooks, like colab. It will display something like this:
output
Cloud servers might not have microphone. That is the reason why you are getting this error.

Implementing skvideo.io.FFmpegReader in Python

Here is my code:
import numpy as np
import matplotlib.pyplot as plt
import skvideo
skvideo.setFFmpegPath("C:/Users/User/PycharmProjects/MachineLearning/venv/lib/site-packages/skvideo/io")
import skvideo.io
input_parameters ={}
output_parameters ={}
reader=skvideo.io.FFmpegReader("Cool_Kids.mp4",inputdict=input_parameters,outputdict=output_parameters)
num_frames,height,width,num_channels =reader.getShape()
print(num_frames, height, width, num_channels)
For analyzing Cool_Kids.mp4 video using skvideo library, before I would use
skvideo.setFFmpegPath("C:/Users/User/PycharmProjects/MachineLearning/venv/lib/site-packages/skvideo/io")
I was getting following error:
AssertionError: Cannot find installation of real FFmpeg (which comes with ffprobe).
Then after researching a bit I found this setFFmpegPath command, but got the same error. What part am I missing? There is this link
Cannot find installation of real FFmpeg (which comes with ffprobe), but I can't do more. What should I do?
You have to install FFmpeg command line tool.
It looks like have to install FFmpeg in your system outside of Python (I can't find a way to download ffmpeg.exe and ffprobe.exe using pip install command).
[Note: Installing FFmpeg in Windows, is different from the way described in your link].
You may also take a look at my answer to the following post.
Recommended solution:
Download FFmpeg (include FFprobe) executable for Windows (download the statically linked version).
Place ffmpeg.exe and ffprobe.exe in the same folder.
For example at: c:/FFmpeg/bin
Set setFFmpegPath to the above folder:
import skvideo
skvideo.setFFmpegPath("C:/FFmpeg/bin")
import skvideo.io
...
Note:
You may choose to place ffmpeg.exe and ffprobe.exe in the folder:
C:/Users/User/PycharmProjects/MachineLearning/venv/lib/site-packages/skvideo/io.

name 'netCDF4' is not defined - python in RStudio

I have recently taken up working on Python 3.6 implemented in RStudio using Miniconda and the library reticulate.
I have installed all the packages that I need using
reticulate::py_install('name of the packages')
when I try to import some .nc data as follow:
import numpy as np
import pandas as pd
import cartopy as cp
from cartopy import crs as ccrs, feature as cfeature
import matplotlib.pyplot as plt
import xarray as xr
import openpyxl
import netCDF4
#%%
adt = xr.open_dataset("G:/Research/ADT.nc")
I get this error:
NameError: name 'netCDF4' is not defined
I have looked at possible solutions, like:
netCDF4 import not being found by Python
but I have installed the package netCDF4 and xarray as the other ones, which do not give any issue (so far)
I had this error in Jupiter Notebook, but then I restarted the Kernel and ran again, and it was fine. I installed netcdf4 using conda.

Pims "Unexpected Keyword Argument" and “invalid ImageJ metadata or corrupted file”

The Context:
I'm trying to use the package trackpy for research. I'm on a Mac and I'm using (mostly) Spyder. I’m following the trackpy walkthrough (https://soft-matter.github.io/trackpy/v0.3.2/tutorial/walkthrough.html) and I’m trying to use it on a tif file. Here’s my code:
#Install trackpy (https://soft-matter.github.io/trackpy/v0.3.2/installation.html)
!conda update conda --yes
!conda install -c conda-forge trackpy --yes
#Use trackpy (trackpy walkthrough: https://soft-matter.github.io/trackpy/v0.3.2/tutorial/walkthrough.html)
from __future__ import division, unicode_literals, print_function
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd
from pandas import DataFrame, Series
import pims
import trackpy as tp
#Install libtif for pims.Tiffstack, per the README (which leads to this link: http://nbviewer.jupyter.org/github/soft-matter/pims/blob/master/examples/loading%20video%20frames.ipynb)
!conda install libtiff --yes
frames = pims.TiffStack("/Users/(my username)/Documents/TSVIEW_Test_5_Grey_1.tif", as_grey=True)
The Problems: When I first wrote the code, I got the error:
TypeError: init() got an unexpected keyword argument 'as_grey'
According to the trackpy walkthrough, it should take as_grey as an argument.
However, when I restarted my computer, I got a different error message (without any code changes as far as I can tell):
/anaconda3/lib/python3.5/site-packages/tifffile/tifffile.py:2113: UserWarning: invalid ImageJ metadata or corrupted file
warnings.warn('invalid ImageJ metadata or corrupted file')
Again, I didn't change the code between restarting it.
Interestingly, when I tried this code in a different python-running software, I got the first error again. I tried it in Google Colab. The only changes I made to the code were using pip instead of conda (Colab only supports pip), downloading the additional dependencies needed for pip per the trackpy install page (https://soft-matter.github.io/trackpy/v0.3.2/installation.html), and uploading the file to Colab per the 'External data: Drive, Sheets, and Cloud Storage' Colab guide.
Things I've tried:
I’ve researched both problems. For the first, I found a page that asked the same problem (pims.Video parameter issue in Spyder), but the only solution provided is "an installation issue" where certain modules are "from the wrong source.” If this is right (is it?), what does it mean? How can I install the modules from the right source?
For the second, I found from the pims website that pims is based on the the tifffile dependency (http://soft-matter.github.io/pims/v0.4/tiff_stack.html). I then found the code that brought the error message (line 2415 from the tifffile.py page https://github.com/scikit-image/scikit-image/blob/master/skimage/external/tifffile/tifffile.py), but I still don’t know how to solve it.
How can I solve these errors?

Categories