欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Python小技巧】获取音频文件时长

程序员文章站 2022-04-09 09:00:25
...
import librosa                                      #pip install librosa
file_url = "media/xx/cj.m4a"                        #音频文件路径
time = librosa.get_duration(filename=file_url)

 

windows下报错:

Error opening 'media/xx/cj.mp3':  File contains data in an unknown format

解决方案:

下载ffmpeg

找到其中的ffmpeg.exe文件,将其复制到项目目录下(文末附带了ffmpeg.exe文件,版本为 4.3.1 windows 64位)

然后打开 项目文件夹\venv\Lib\site-packages\audioread\ffdec.py

# COMMANDS = ('ffmpeg', 'avconv')                 #源代码
COMMANDS = ('./ffmpeg.exe', 'avconv')             #替换后的代码

linux下pip install librosa报错

Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Requirement already satisfied: librosa in ./.virtualenvs/music/lib/python3.6/site-packages (0.8.0)
Collecting audioread>=2.0.0
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/2e/0b/940ea7861e0e9049f09dcfd72a90c9ae55f697c17c299a323f0148f913d2/audioread-2.1.8.tar.gz (21 kB)
    ERROR: Command errored out with exit status 1:
     command: /root/.virtualenvs/music/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-bsstrwt7/audioread/setup.py'"'"'; __file__='"'"'/tmp/pip-install-bsstrwt7/audioread/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-qliwgemh
         cwd: /tmp/pip-install-bsstrwt7/audioread/
    Complete output (33 lines):
    WARNING: The repository located at mirrors.cloud.aliyuncs.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.cloud.aliyuncs.com'.
    ERROR: Could not find a version that satisfies the requirement pytest-runner (from versions: none)
    ERROR: No matching distribution found for pytest-runner
    Traceback (most recent call last):
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/setuptools/installer.py", line 128, in fetch_build_egg
        subprocess.check_call(cmd)
      File "/usr/lib/python3.6/subprocess.py", line 311, in check_call
        raise CalledProcessError(retcode, cmd)
    subprocess.CalledProcessError: Command '['/root/.virtualenvs/music/bin/python', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpmlr7fab1', '--quiet', '--index-url', 'http://mirrors.cloud.aliyuncs.com/pypi/simple/', 'pytest-runner']' returned non-zero exit status 1.
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-bsstrwt7/audioread/setup.py", line 58, in <module>
        'Programming Language :: Python :: 3.6',
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/setuptools/__init__.py", line 162, in setup
        _install_setup_requires(attrs)
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/setuptools/__init__.py", line 157, in _install_setup_requires
        dist.fetch_build_eggs(dist.setup_requires)
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/setuptools/dist.py", line 702, in fetch_build_eggs
        replace_conflicting=True,
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/pkg_resources/__init__.py", line 781, in resolve
        replace_conflicting=replace_conflicting
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/pkg_resources/__init__.py", line 1064, in best_match
        return self.obtain(req, installer)
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/pkg_resources/__init__.py", line 1076, in obtain
        return installer(requirement)
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/setuptools/dist.py", line 758, in fetch_build_egg
        return fetch_build_egg(self, req)
      File "/root/.virtualenvs/music/lib/python3.6/site-packages/setuptools/installer.py", line 130, in fetch_build_egg
        raise DistutilsError(str(e)) from e
    distutils.errors.DistutilsError: Command '['/root/.virtualenvs/music/bin/python', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpmlr7fab1', '--quiet', '--index-url', 'http://mirrors.cloud.aliyuncs.com/pypi/simple/', 'pytest-runner']' returned non-zero exit status 1.
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

直接将windows上的\venv\Lib\site-packages\audioread上传到服务器的虚拟环境

ffmpeg.exe 下载 https://download.csdn.net/download/Swing0_0/12908760