简体   繁体   English

如何将视频文件传递给 Flask 中的 FFMPEG 以便将其转换为 MP4?

[英]How would I pass a video file to FFMPEG in Flask in order to convert it to MP4?

I'm building a video sharing site and everything has been going smoothly so far until this.我正在建立一个视频共享网站,到目前为止一切都很顺利。 I want to pass the video file I'm uploading to FFMPEG to convert it to MP4 and to a specific resolution (480p).我想将我上传的视频文件传递给 FFMPEG 以将其转换为 MP4 和特定分辨率 (480p)。 I've been spending over an hour trying to figure out how to do this to no success.我已经花了一个多小时试图弄清楚如何做到这一点,但没有成功。 Could anybody help?有人可以帮忙吗? Here is my code:这是我的代码:

Code for uploading:上传代码:

@app.route("/upload", methods=["GET", "POST"])
@login_required
def upload():
    if request.method == "POST":
        if "video" not in request.files:
            print("error whole uploading")
            return redirect(request.url)
        video = request.files["video"]
        if video.filename == "":
            print("no file uplaoded")
            return redirect(request.url)
        if video and filename_check(video.filename, type="video"):
            filename = secure_filename(video.filename)
            file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
            print(file_path)
            video.save(file_path)

            print("upload success")

            return redirect(request.url)
            
    return render_template("upload.html")

init .py:初始化.py:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
import os

app = Flask(__name__)

UPLOAD_FOLDER = os.path.join(app.root_path, 'uploads')
ALLOWED_VIDEO_EXTENTIONS = {"mp4", "webm"}
ALLOWED_IMAGE_EXTENTIONS = {"mp4", "webm"}

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def filename_check(filename, type):
    if type == "video":
        return "." in filename and \
            filename.rsplit(".", 1)[1].lower() in ALLOWED_VIDEO_EXTENTIONS
    else:
        return "." in filename and \
            filename.rsplit(".", 1)[1].lower() in ALLOWED_IMAGE_EXTENTIONS

app.config["SECRET_KEY"] = "newtube"  # do not reveal to ANYONE dumbass

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
db = SQLAlchemy(app)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "signin"

import newtube.views

After saved video, using pydub to convert it to MP4 format:保存视频后,使用pydub将其转换为 MP4 格式:

from pydub import AudioSegment
def convert_to_mp4(file_path):
  tmp_audio = AudioSegment.from_file(file_path)
  tmp_audio.export(file_path, format='wav', parameters=["-vf", "scale=-1:480"])
  return file_path

I've found a solution using MoviePy:我找到了使用 MoviePy 的解决方案:

from moviepy.editor import *
...
converted = VideoFileClip(file_path)
converted.resize(width=480)
converted.write_videofile(file_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有 ffmpeg 的情况下将 python 上的视频转换为.mp4? - How to convert video on python to .mp4 without ffmpeg? 我在 .sec 路径中有一个文件,当我使用 ffmpeg 将其转换为 mp4 或 avi 格式时,视频输出速度非常快 - I have a file in the .sec path, when I convert it to mp4 or avi format with ffmpeg, the video comes out very fast 如何使用 FFmpeg 和 python 中的子进程模块将.mp4视频文件转换为.yuv(YUV420),反之亦然? - How to convert .mp4 video file to .yuv (YUV420) and vice-versa using FFmpeg and subprocess module in python? 如何使用 Flask 提供 MP4 视频 - How to Serve MP4 Video with Flask 如何在脚本(pycharm)中使用 ffmpeg 将 webm/mp4 文件转换为 mp3? - How to convert webm/mp4 file to mp3 with ffmpeg in a script (pycharm)? 如何使用 ffmpeg (python) 将 mp4 转换为 mp3 - How to convert mp4 into mp3 using ffmpeg (python) 如何使用 python 打开 MP4 视频文件? - How do I open an MP4 video file with python? 使用 ffmpeg 将 video.ts 文件转换为 video.mp4 - Convert video.ts file into video.mp4 using ffmpeg 我正在尝试使用 MoviePy 模块将视频 mp4 文件转换为音频 mp3 格式,但它向我显示 Nonetype 错误 - I am trying to convert an video mp4 file to Audio mp3 format by using MoviePy module but it is showing me Nonetype error ffmpeg将图像转换为mp4错误 - ffmpeg convert images to mp4 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM