跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Python] 这个场景下,在用户下载完转换好的文件再删除临时文件的最佳实际是什么?

发表于
from flask import Flask, request, send_file, abort
import ffmpeg
import os
import tempfile
import time

app = Flask(__name__)

@app.route('/audio_appledevices/<name>.m4a', methods=['GET'])
def convert_audio(name):
    ogg_path = f'./audio/{name}.ogg'
    if not os.path.exists(ogg_path):
        abort(404)
    with tempfile.NamedTemporaryFile(delete=False, suffix='.m4a') as temp_file:
        m4a_path = temp_file.name
    try:
        ffmpeg.input(ogg_path).output(m4a_path, codec='alac').run(overwrite_output=True)
        response = send_file(m4a_path, as_attachment=True, download_name=f'{name}.m4a', mimetype='audio/mp4')
        @response.call_on_close
        def cleanup():
            time.sleep(1)
            if os.path.exists(m4a_path):
                os.remove(m4a_path)
        return response
    except ffmpeg.Error as e:
        abort(500)


if __name__ == '__main__':
    app.run(host='127.0.0.1', port=3389)

除非另开一个线程,不管怎么写临时文件都不能被自动删掉,要不就是 ffmpeg 写不进文件,因为临时文件被占用。 因为 ffmpeg 需要 seekable 的流,所以输出到 IO 流不可行。

请先测试再回复!我让 GPT-4 写了几十个版本,没有一个能用。

请先测试再回复!我让 GPT-4 写了几十个版本,没有一个能用。

请先测试再回复!我让 GPT-4 写了几十个版本,没有一个能用。

请先测试再回复!我让 GPT-4 写了几十个版本,没有一个能用。

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.