Loading a File from Google Bucket into PyDub AudioSegment

Viewed 980

I have been trying for a while to load a .mp3 file which exists in a Google Bucket into Pydub. Here is my code

f = io.BytesIO()
storage_client = storage.Client()
bucket_name="my-bucket"
bucket = storage_client.get_bucket(bucket_name) 
blob = bucket.blob(file_path)
blob.download_to_file(f)

currentAudio=AudioSegment.from_mp3(f)

Here is the Path to the File https://storage.cloud.google.com/written-audio-files/10530c70-52af-4ed7-a2ad-146738141b41.mp3

So this file exists and is also downloaded correctly. When the last line

currentAudio=AudioSegment.from_mp3(f) 

is executed, I get an FFMPEG error.

pydub.exceptions.CouldntDecodeError: Decoding failed. ffmpeg returned error code: 1

Output from ffmpeg/avlib:

b'ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers\n  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)\n  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared\n  libavutil      55. 78.100 / 55. 78.100\n  libavcodec     57.107.100 / 57.107.100\n  libavformat    57. 83.100 / 57. 83.100\n  libavdevice    57. 10.100 / 57. 10.100\n  libavfilter     6.107.100 /  6.107.100\n  libavresample   3.  7.  0 /  3.  7.  0\n  libswscale      4.  8.100 /  4.  8.100\n  libswresample   2.  9.100 /  2.  9.100\n  libpostproc    54.  7.100 / 54.  7.100\n[mp3 @ 0x556a1d4c7820] Failed to read frame size: Could not seek to 1026.\npipe:: Invalid argument\n'

To, me it appears that when a BytesIO object is given to the function from_mp3, it expects a Physical File versus an In Memory BytesIO object.

I am executing this on a Google Cloud function , where I probably will not have any local storage. Plus, it will be time consuming to save the file, and then reload the file from /tmp

Saving to local filesystem and reopening the file works as expected on my local computer where I am currently testing the program. How do we circumvent the issue?

** Update ** Passing the downloaded file to ffmpeg yields the following output

ffmpeg -i 10530c70-52af-4ed7-a2ad-146738141b41.mp3 -hide_banner
[mp3 @ 0x561b77222760] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from '10530c70-52af-4ed7-a2ad-146738141b41.mp3':
  Duration: 00:00:02.09, start: 0.000000, bitrate: 32 kb/s
    Stream #0:0: Audio: mp3, 24000 Hz, mono, s16p, 32 kb/s
At least one output file must be specified
2 Answers

I think there's something wrong with your MP3 file, I can't play it and FFMPEG is confused about what file type it is:

$ ffmpeg -i 10530c70-52af-4ed7-a2ad-146738141b41.mp3 -hide_banner
[mp3 @ 0x7fa661000000] Format mp3 detected only with low score of 1, misdetection possible!
[mp3 @ 0x7fa661000000] Failed to read frame size: Could not seek to 58401.
10530c70-52af-4ed7-a2ad-146738141b41.mp3: Invalid argument

Save your file on the tmp directory and then provide the Path to AudioSegment().

tmpdir=tempfile.gettempdir() # prints the current temporary directory
tempFilePath=tmpdir+"/"+file_path
storage_client = storage.Client()
bucket_name="my-bucket"
bucket = storage_client.get_bucket(bucket_name)         
blob = bucket.blob(file_path)   
blob.download_to_filename(tempFilePath)     
currentAudio=AudioSegment.from_file(tempFilePath, format="mp3")
Related