question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upload to static file in flask project

See original GitHub issue
def create():
    if request.method == 'POST':
            file = request.files["video"]
            stream = ffmpeg.input(file)
            stream = ffmpeg.hflip(stream)
            stream = ffmpeg.output(stream, app.root_path + '/' + app.config['UPLOAD_FOLDER'] + '/videos/dd.mp4')
            ffmpeg.run(stream)

-filter_complex: No such file or directory (error from ffmpeg)

Any solution for my problem? I try change directory and check directory before pass a path but error still same.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
depaucommented, Mar 8, 2018

I don’t know exactly how Flask deals with POSTed files but I think it gives you a file-like object you can read from. If so, you should call ffmpeg manually (i.e. p = subprocess.Popen(stream.compile(), stdin=subprocess.PIPE)), use stdin as input in ffmpeg (use pipe:1 as the file name, see this), then write to the pipe with a loop, i.e. (code not tested)

CHUNK_SIZE = 1024
data = file.read(CHUNK_SIZE)
while data:
    p.stdin.write(data)
    data = file.read(CHUNK_SIZE)

Check Flask’s documentation to see how to stream POST data like that.

0reactions
krrishdholakiacommented, Feb 7, 2021

@Honghe Did some further digging,

here’s what i’m getting:

pipe:0: Invalid data found when processing input

This is what my function looks like:

` @app.route(“/”, methods=[‘GET’, ‘POST’]) def hello_world(): print("request: ", request) print("request.files: ", request.files)

""" Convert Flask mp3 file to wav and save
Flask request.file is wrap request.file.stream
And request.file.stream ia type: tempfile.SpooledTemporaryFile
tempfile.SpooledTemporaryFile: Temporary file wrapper, specialized to switch from BytesIO \
or StringIO to a real file when it exceeds a certain size or when a fileno is needed.
:param file:
:return:
"""
file = request.files['file']
f_path = "upload_file.wav"
stream = ffmpeg.input('pipe:0')
stream = ffmpeg.output(stream, f_path, acodec='pcm_s16le', ac=1, ar='16k')
# TODO, prompt and log when overwrite
stream = ffmpeg.overwrite_output(stream)
# call ffmpeg manually (i.e. p = subprocess.Popen(stream.compile(), stdin=subprocess.PIPE)),

# stream.compile() Build command-line for invoking ffmpeg.
# subprocess.Popen() Execute a child program in a new process
p = subprocess.Popen(stream.compile(), stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
CHUNK_SIZE = 1024
data = file.read(CHUNK_SIZE)
print("data type: ", type(data))
p.stdin.write(data)
# while data:
#     p.stdin.write(data)
#     data = file.read(CHUNK_SIZE)
#     print("data type: ", type(data))
print(p.communicate())
# upload.upload_file_main('upload_file.wav')
return "<p>Hello, World!</p>"

`

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to serve static files in Flask - python - Stack Overflow
The simplest way is create a static folder inside the main project folder. Static folder containing .css files.
Read more >
Serving Static Files with Flask - Stack Abuse
In this article we saw how you can easily serve static assets using Flask. You can serve JavaScript, CSS, images as well as...
Read more >
Static Files — Flask Documentation (2.2.x)
Static Files ¶ ; static view that takes a path relative to the ; flaskr/static directory and serves it. The ; base.html template...
Read more >
Flask static files (Example) - Python Tutorial
In your programs directory, create a new directory named static. In this directory you can place images, javascript files, css files and many...
Read more >
Uploading files using Flask - Medium
Uploading files using Flask · $ python app.py. Then open your browser and go to localhost:5000 and you will be able to see...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found