У меня проблема с воспроизведением файла в браузере с помощью pydub. Я думаю, что функция работает, но я просто не могу указать правильный путь к ней или чего-то еще не хватает. Хотел бы получить вашу помощь!
Я получаю сообщение об ошибке pydob: pydub.exceptions.CouldntDecodeError: Ошибка декодирования. ffmpeg вернул код ошибки: 1
Вот полученная мной ошибка:
[2020-05-28 16:04:33,023] ERROR in app: Exception on /overview [POST]
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/Micha/Documents/GitHub/podprod/app.py", line 109, in overview
sound = AudioSegment.from_file(filepath)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pydub/audio_segment.py", line 723, in from_file
raise CouldntDecodeError(
pydub.exceptions.CouldntDecodeError: Decoding failed. ffmpeg returned error code: 1
Output from ffmpeg/avlib:
b'ffmpeg version 4.2.3 Copyright (c) 2000-2020 the FFmpeg developers\n built with Apple clang version 11.0.3 (clang-1103.0.32.59)\n configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.3 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags=-fno-stack-check --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack\n libavutil 56. 31.100 / 56. 31.100\n libavcodec 58. 54.100 / 58. 54.100\n libavformat 58. 29.100 / 58. 29.100\n libavdevice 58. 8.100 / 58. 8.100\n libavfilter 7. 57.100 / 7. 57.100\n libavresample 4. 0. 0 / 4. 0. 0\n libswscale 5. 5.100 / 5. 5.100\n libswresample 3. 5.100 / 3. 5.100\n libpostproc 55. 5.100 / 55. 5.100\n/Users/Micha/documents/github/podprod/uploads/test2.wav: Invalid data found when processing input\n'
Это маршрут Flask с функцией, которую я пишу .
@app.route("/overview", methods=["GET", "POST"])
def overview():
entries = []
# Open a file
path = app.config["FILE_UPLOADS"]
with os.scandir(path) as dirs:
for entry in dirs:
entries.append(entry.name)
if request.method == "POST":
filename = request.form['filename']
filepath = os.path.join(app.config["FILE_UPLOADS"], filename)
# Play the sound
sound = AudioSegment.from_file(filepath)
play(sound)
return render_template('overview.html', entries=entries)
Вот шаблон HTML:
{% extends 'main_template.html' %}
{% block title %}PodProd Podcast Overview{% endblock %}
{% block main %}
<div class="container">
<h1>Here is an overview of your files</h1>
<table class="table table-striped">
<tr>
<th>Filename</th>
<th>Action</th>
</tr>
{% for result in entries %}
{% if ".wav" in result %}
<tr>
<td>{{ result }}</td>
<td><form action="" method="POST"> <button type="submit" name="filename" class="btn btn-primary" value={{ result }}>Play</button></form>
</tr>
{% endif %}
{% endfor %}
</table>
</div>
{% endblock %}