从url读取pydub AudioSegment。BytesIO returning”一词;OSError[Errno 2]没有这样的文件或目录;仅在heroku上;在本地主机上很好

从url读取pydub AudioSegment。BytesIO returning”一词;OSError[Errno 2]没有这样的文件或目录;仅在heroku上;在本地主机上很好,heroku,ffmpeg,youtube-dl,pydub,bytesio,Heroku,Ffmpeg,Youtube Dl,Pydub,Bytesio,为有相同错误的人编辑1:安装ffmpeg确实解决了BytesIO错误 为仍然愿意提供帮助的人编辑1:现在我的问题是,当我使用AudioSegment.export(“filename.mp3”,format=“mp3”)时,文件已生成,但大小为0字节——详细信息如下(如“编辑1”) 编辑2:所有问题现在都已解决 可以使用BytesIO将文件作为音频段读入 我发现BuildPack可以确保在我的应用程序上正确安装了ffmpeg,并且对导出正确的mp3文件支持不足 回答如下 原始问题 我让p

为有相同错误的人编辑1:安装ffmpeg确实解决了BytesIO错误

为仍然愿意提供帮助的人编辑1:现在我的问题是,当我使用AudioSegment.export(“filename.mp3”,format=“mp3”)时,文件已生成,但大小为0字节——详细信息如下(如“编辑1”)


编辑2:所有问题现在都已解决

  • 可以使用BytesIO将文件作为音频段读入
  • 我发现BuildPack可以确保在我的应用程序上正确安装了ffmpeg,并且对导出正确的mp3文件支持不足
回答如下


原始问题 我让pydub在本地很好地工作,根据url中的参数裁剪特定的mp3文件。 (?开始时间=3.8,结束时间=5.1)

当我运行
foreman start
时,在本地主机上一切看起来都很好。html呈现得很好。 views.py中的关键行包括使用

url = "https://s3.amazonaws.com/shareducate02/The_giving_tree__by_Alex_Blumberg__sponsored_by_mailchimp-short.mp3"
mp3 = urllib.urlopen(url).read() # inspired by http://nbviewer.ipython.org/github/ipython-books/cookbook-code/blob/master/notebooks/chapter11_image/06_speech.ipynb
original=AudioSegment.from_mp3(BytesIO(mp3))  # AudioSegment.from_mp3 is a pydub command, see http://pydub.com
section = original[start_time_ms:end_time_ms]
这一切都很好。。。直到我推到heroku(django应用程序)并在线运行它。 然后,当我现在在herokuapp.com上加载相同的页面时,我得到了这个错误

OSError at /path/to/page
[Errno 2] No such file or directory
Request Method: GET
Request URL:    http://my.website.com/path/to/page?start_time=3.8&end_time=5
Django Version: 1.6.5
Exception Type: OSError
Exception Value:    
[Errno 2] No such file or directory
Exception Location: /app/.heroku/python/lib/python2.7/subprocess.py in _execute_child, line 1327
Python Executable:  /app/.heroku/python/bin/python
Python Version: 2.7.8
Python Path:    
['/app',
 '/app/.heroku/python/bin',
 '/app/.heroku/python/lib/python2.7/site-packages/setuptools-5.4.1-py2.7.egg',
 '/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.36-py2.7.egg',
 '/app/.heroku/python/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg',
 '/app',
 '/app/.heroku/python/lib/python27.zip',
 '/app/.heroku/python/lib/python2.7',
 '/app/.heroku/python/lib/python2.7/plat-linux2',
 '/app/.heroku/python/lib/python2.7/lib-tk',
 '/app/.heroku/python/lib/python2.7/lib-old',
 '/app/.heroku/python/lib/python2.7/lib-dynload',
 '/app/.heroku/python/lib/python2.7/site-packages',
 '/app/.heroku/python/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']


Traceback:
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/evernote/views.py" in finalize
  105.       original=AudioSegment.from_mp3(BytesIO(mp3))
File "/app/.heroku/python/lib/python2.7/site-packages/pydub/audio_segment.py" in from_mp3
  318.         return cls.from_file(file, 'mp3')
File "/app/.heroku/python/lib/python2.7/site-packages/pydub/audio_segment.py" in from_file
  302.         retcode = subprocess.call(convertion_command, stderr=open(os.devnull))
File "/app/.heroku/python/lib/python2.7/subprocess.py" in call
  522.     return Popen(*popenargs, **kwargs).wait()
File "/app/.heroku/python/lib/python2.7/subprocess.py" in __init__
  710.                                 errread, errwrite)
File "/app/.heroku/python/lib/python2.7/subprocess.py" in _execute_child
  1327.                 raise child_exception

我已经注释掉了一些原文,以说服自己,单行
original=AudioSegment.from_mp3(BytesIO(mp3))
确实是问题的根源。。。但这在本地并不是问题

views.py中的完整函数如下开始:

from django.shortcuts import render, get_object_or_404 
from django.http import HttpResponseRedirect #, Http404, HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic
import pydub
# Maybe only need: 
from pydub import AudioSegment # == see below
from time import gmtime, strftime

import boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key

# http://nbviewer.ipython.org/github/ipython-books/cookbook-code/blob/master/notebooks/chapter11_image/06_speech.ipynb
import urllib
from io import BytesIO
# import numpy as np
# import scipy.signal as sg
# import pydub # mentioned above already
# import matplotlib.pyplot as plt
# from IPython.display import Audio, display
# import matplotlib as mpl
# %matplotlib inline

import os
# from settings import AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_BUCKET_NAME
AWS_ACCESS_KEY = os.environ.get('AWS_ACCESS_KEY') # there must be a better way?
AWS_SECRET_KEY = os.environ.get('AWS_SECRET_KEY')
AWS_BUCKET_NAME = os.environ.get('S3_BUCKET_NAME')

# http://stackoverflow.com/questions/415511/how-to-get-current-time-in-python

boto_conn = S3Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY)
bucket = boto_conn.get_bucket(AWS_BUCKET_NAME)
s3_url_format = 'https://s3.amazonaws.com/shareducate02/{end_path}'
特别是我访问页面时调用的views.py中的视图:

def finalize(request):

    start_time = request.GET.get('start_time')

    end_time = request.GET.get('end_time')

    original_file = "https://s3.amazonaws.com/shareducate02/The_giving_tree__by_Alex_Blumberg__sponsored_by_mailchimp-short.mp3"


    if start_time:

      # original=AudioSegment.from_mp3(original_file)  #...that didn't work 
      # but this works below:

      # next three uncommented lines from http://nbviewer.ipython.org/github/ipython-books/cookbook-code/blob/master/notebooks/chapter11_image/06_speech.ipynb
      # python 2.x
      url = original_file
      # req = urllib.Request(url, headers={'User-Agent': ''}) # Note: I commented out this because I got error that "Request" did not exist
      mp3 = urllib.urlopen(url).read()
      # That's for my 2.7

      # If I ever upgrade to python 3.x, would need to change it to:
      # req = urllib.request.Request(url, headers={'User-Agent': ''}) 
      # mp3 = urllib.request.urlopen(req).read()
      # as per instructions on http://nbviewer.ipython.org/github/ipython-books/cookbook-code/blob/master/notebooks/chapter11_image/06_speech.ipynb

      original=AudioSegment.from_mp3(BytesIO(mp3))
      # original=AudioSegment.from_mp3("static/givingtree.mp3") # alternative that works locally (on laptop) but no use for heroku

      start_time_ms = int(float(start_time) * 1000)
      if end_time:
        end_time_ms = int(float(end_time) * 1000)
      else:
        end_time_ms = int(float(original.duration_seconds) * 1000)
      duration_ms = end_time_ms - start_time_ms
      # duration = end_time - start_time
      duration = duration_ms/1000

   #   section = original[start_time_ms:end_time_ms]
   #   section_with_fading = section.fade_in(100).fade_out(100)

      clip = "demo-"
      number = strftime("%Y-%m-%d_%H-%M-%S", gmtime())
      clip += number
      clip += ".mp3" 

      # DON'T BOTHER writing locally:
      # clip_with_path = "evernote/static/"+clip
      # section_with_fading.export(clip_with_path, format = "mp3")

   #   tempclip = section_with_fading.export(format = "mp3")

      # commented out while de-bugging, but was working earlier if run on localhost
      # c = boto.connect_s3()
      # b = c.get_bucket(S3_BUCKET_NAME)  # as defined above
      # k = Key(b)
      # k.key=clip
      # # k.set_contents_from_filename(clip_with_path)
      # k.set_contents_from_file(tempclip)
      # k.set_acl('public-read')
      clip_made = True
    else: 
      duration = 0.0
      clip_made = False
      clip = ""
    context = {'original_file':original_file, 'new_file':clip, 'start_time': start_time, 'end_time':end_time, 'duration':duration, 'clip_made':clip_made} 
    return render(request, 'finalize.html' , context) 
有什么建议吗

潜在相关: 我在本地安装了ffmpeg

但由于不了解构建包,我们无法将其安装到heroku上。我刚才试过(
http://stackoverflow.com/questions/14407388/how-to-install-ffmpeg-for-a-django-app-on-heroku
https://github.com/shunjikonishi/heroku-buildpack-ffmpeg
)但到目前为止,ffmpeg还不能在heroku上运行(当我执行“heroku运行ffmpeg--version”时,ffmpeg无法识别) …你认为这就是原因吗

如果能给出这样的答案,我将不胜感激,因为我将在这里转圈:

  • “我认为ffmpeg确实是您的问题。请更努力地解决这个问题,并将其安装到heroku上”
  • “事实上,我认为这就是比泰西奥不为你工作的原因:…”
  • “无论如何,你的方法很糟糕……如果你想读入音频文件以使用pydub进行处理,你应该这样做:……”(因为我只是第一次通过pydub进行黑客攻击……我的方法可能很差)

  • 编辑1 现在已安装ffmpeg(例如,我可以输出wav文件)

    然而,我不能创建mp3文件,仍然。。。或者更准确地说,我可以,但是文件大小是零

    (venv-app)moriartymacbookair13:getstartapp macuser$ heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git 
    Setting config vars and restarting awe01... done, v93
    BUILDPACK_URL: https://github.com/ddollar/heroku-buildpack-multi.git
    (venv-app)moriartymacbookair13:getstartapp macuser$ vim .buildpacks 
    (venv-app)moriartymacbookair13:getstartapp macuser$ cat .buildpacks 
    https://github.com/shunjikonishi/heroku-buildpack-ffmpeg.git
    https://github.com/heroku/heroku-buildpack-python.git
    (venv-app)moriartymacbookair13:getstartapp macuser$ git add --all
    (venv-app)moriartymacbookair13:getstartapp macuser$ git commit -m "need multi, not just ffmpeg, so adding back in multi + shun + heroku, with trailing .git in .buildpacks file"
    [master cd99fef] need multi, not just ffmpeg, so adding back in multi + shun + heroku, with trailing .git in .buildpacks file
     1 file changed, 2 insertions(+), 2 deletions(-)
    (venv-app)moriartymacbookair13:getstartapp macuser$ git push heroku master
    Fetching repository, done.
    Counting objects: 5, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 372 bytes | 0 bytes/s, done.
    Total 3 (delta 2), reused 0 (delta 0)
    
    -----> Fetching custom git buildpack... done
    -----> Multipack app detected
    =====> Downloading Buildpack: https://github.com/shunjikonishi/heroku-buildpack-ffmpeg.git
    =====> Detected Framework: ffmpeg
    -----> Install ffmpeg
           DOWNLOAD_URL =  http://flect.github.io/heroku-binaries/libs/ffmpeg.tar.gz
           exporting PATH and LIBRARY_PATH
    =====> Downloading Buildpack: https://github.com/heroku/heroku-buildpack-python.git
    =====> Detected Framework: Python
    -----> Installing dependencies with pip
           Cleaning up...
    
    -----> Preparing static assets
           Collectstatic configuration error. To debug, run:
           $ heroku run python ./example/manage.py collectstatic --noinput
    
    Using release configuration from last framework (Python).
    -----> Discovering process types
           Procfile declares types -> web
    
    -----> Compressing... done, 198.1MB
    -----> Launching... done, v94
           http://[redacted].herokuapp.com/ deployed to Heroku
    
    To git@heroku.com:awe01.git
       78d6b68..cd99fef  master -> master
    (venv-app)moriartymacbookair13:getstartapp macuser$ heroku run ffmpeg
    Running `ffmpeg` attached to terminal... up, run.6408
    ffmpeg version git-2013-06-02-5711e4f Copyright (c) 2000-2013 the FFmpeg developers
      built on Jun  2 2013 07:38:40 with gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
      configuration: --enable-shared --disable-asm --prefix=/app/vendor/ffmpeg
      libavutil      52. 34.100 / 52. 34.100
      libavcodec     55. 13.100 / 55. 13.100
      libavformat    55.  8.102 / 55.  8.102
      libavdevice    55.  2.100 / 55.  2.100
      libavfilter     3. 74.101 /  3. 74.101
      libswscale      2.  3.100 /  2.  3.100
      libswresample   0. 17.102 /  0. 17.102
    Hyper fast Audio and Video encoder
    usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
    
    Use -h to get full help or, even better, run 'man ffmpeg'
    (venv-app)moriartymacbookair13:getstartapp macuser$ heroku run bash
    Running `bash` attached to terminal... up, run.9660
    ~ $ python
    Python 2.7.8 (default, Jul  9 2014, 20:47:08) 
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pydub
    >>> from pydub import AudioSegment
    >>> exit()
    ~ $ which ffmpeg
    /app/vendor/ffmpeg/bin/ffmpeg
    ~ $ python 
    
    Python 2.7.8 (default, Jul  9 2014, 20:47:08) 
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pydub
    >>> from pydub import AudioSegment
    >>> AudioSegment.silent(5000).export("/tmp/asdf.mp3", "mp3")
    <open file '/tmp/asdf.mp3', mode 'wb+' at 0x7f9a37d44780>
    >>> exit ()
    ~ $ cd /tmp/
    /tmp $ ls
    asdf.mp3
    /tmp $ open asdf.mp3
    bash: open: command not found
    /tmp $ ls -lah
    total 8.0K
    drwx------  2 u36483 36483 4.0K 2014-10-22 04:14 .
    drwxr-xr-x 14 root   root  4.0K 2014-09-26 07:08 ..
    -rw-------  1 u36483 36483    0 2014-10-22 04:14 asdf.mp3
    
    最后,我尝试导出一个.wav文件来检查pydub是否至少正常工作

    /tmp $ python
    Python 2.7.8 (default, Jul  9 2014, 20:47:08) 
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pydub
    >>> from pydub import AudioSegment
    >>> pydub.AudioSegment.ffmpeg = "/app/vendor/ffmpeg/bin/ffmpeg"
    >>> AudioSegment.silent(1300).export("/tmp/heroku_wav_silence01.wav", format="wav")
    <open file '/tmp/heroku_wav_silence01.wav', mode 'wb+' at 0x7fa33cbf3780>
    >>> exit()
    /tmp $ ls
    asdf.mp3  herokuSilence03.mp3  herokuSilence.mp3  heroku_wav_silence01.wav
    /tmp $ ls -lah
    total 40K
    drwx------  2 u36483 36483 4.0K 2014-10-22 04:42 .
    drwxr-xr-x 14 root   root  4.0K 2014-09-26 07:08 ..
    -rw-------  1 u36483 36483    0 2014-10-22 04:14 asdf.mp3
    -rw-------  1 u36483 36483    0 2014-10-22 04:31 herokuSilence03.mp3
    -rw-------  1 u36483 36483    0 2014-10-22 04:29 herokuSilence.mp3
    -rw-------  1 u36483 36483  29K 2014-10-22 04:42 heroku_wav_silence01.wav
    /tmp $ 
    
    信息链接是,它太具体的mp3故障,所以可能这两个问题是相关的


    编辑2
    请参阅答案,所有问题均已解决

    Pydub使用ffmpeg对wav以外的所有格式进行编码/解码。因此,第一个问题是在heroku上安装ffmpeg

    您可能会发现,使用
    heroku运行bash
    可以
    cd
    查找ffmpeg二进制文件(请在
    /app/.heroku/vendor
    中尝试)

    如果是这种情况,您可以显式指定pydub的位置,如下所示:

    import pydub
    
    
    pydub.AudioSegment.converter = "/app/.heroku/vendor/ffmpeg/bin/ffmpeg" # or wherever you find it
    
    编辑 我能够做到以下几点:

    在空git回购的根目录中创建一个
    requirements.txt

    requirements.txt

    pydub
    
    添加这个并推到heroku

    跑步:

    heroku config:add BUILDPACK_URL=https://github.com/integricho/heroku-buildpack-python-ffmpeg.git
    
    然后:

    在壳里

    $ which ffmpeg
    /app/.heroku/vendor/ffmpeg/bin/ffmpeg
    
    $ python
    >>> from pydub import AudioSegment
    >>> AudioSegment.silent(5000).export("/tmp/asdf.mp3", "mp3")
    <open file '/tmp/asdf.mp3', mode 'wb+' at 0x7ffa8aac0390>
    
    $哪个ffmpeg
    /app/.heroku/vendor/ffmpeg/bin/ffmpeg
    $python
    >>>从pydub导入音频段
    >>>AudioSegment.silent(5000)导出(“/tmp/asdf.mp3”、“mp3”)
    

    之后,ffmpeg位于:
    /app/.heroku/vendor/ffmpeg/bin/ffmpeg
    所有问题都已解决,谢谢

    我现在可以使用BytesIO从url读取音频片段。 我现在可以在处理后导出mp3或wav

    使用此处推荐的包解决了ffmpeg问题:
    http://blog.pogoapp.com/youtube-mp3-with-node-js-and-ffmpeg/
    (将“nodejs”替换为我的语言“python”) 这里推荐的ffmpeg包()已经包含了我所需要的lame支持 由于某种原因,我没有完全为我做好这项工作

    我还必须在requirements.txt中添加“ffprobe”以允许youtube dl正常运行(我在这里提到了这一点,因为它之前也抱怨lame丢失……添加ffprobe是使其正常运行的第二步)


    我的答案全文如下:

    @Jiarro,非常感谢你的回复。那个特定的buildpack对我不起作用,但我现在确实设法让ffmpeg工作了,使用了一种类似的方法$cat.buildpacks,它允许我在保持python heroku配置的同时获取ffmpeg:add buildpack_URL=now ffmpeg的行为就像它在工作一样,但当它编写mp3时,导出的文件大小为零字节。我将把上面的完整输出粘贴到我的question@Jiarro,buildpack让ffmpeg工作得足够好,我可以使用heroku上的youtube dl下载视频,但mp3仍然失败(即,我仍然无法pydub导出mp3,也无法使用youtube dl提取音频)。integricho的构建似乎没有我需要的mp3/lame支持。我添加了我自己的答案,以显示我最终决定使用的buildpack,包括ffmpeg和lame支持。再次感谢您的支持
    original=AudioSegment。从mp3(BytesIO(mp3))
    (即向BytesIO播放)是我的外卖。。。
    pydub
    
    heroku config:add BUILDPACK_URL=https://github.com/integricho/heroku-buildpack-python-ffmpeg.git
    
    heroku run bash
    
    $ which ffmpeg
    /app/.heroku/vendor/ffmpeg/bin/ffmpeg
    
    $ python
    >>> from pydub import AudioSegment
    >>> AudioSegment.silent(5000).export("/tmp/asdf.mp3", "mp3")
    <open file '/tmp/asdf.mp3', mode 'wb+' at 0x7ffa8aac0390>