Пример #1
0
def add_video():
    """添加番剧视频"""
    form = BangumiAddVideoForm().validate_for_api()
    bangumi = form.obj
    with db.auto_commit():
        video = Video()
        video.name = form.name.data
        try:
            file = request.files[form.url.name]
            if not allowed_video_file(file.filename):
                return ReturnObj.get_response(
                    ReturnEnum.VIDEO_TYPE_ERROR.value,
                    "只允许上传mp4 avi flv wmv格式")
            file_url = secure_filename(file.filename)
            url = change_filename(file_url)
            file.save(os.path.join(current_app.config["VIDEO_DIR"], url))
            try:
                video_clip = VideoFileClip(
                    os.path.join(current_app.config["VIDEO_DIR"], url))
                video.length = video_clip.duration
                video_clip.reader.close()
                video_clip.audio.reader.close_proc()
            except Exception as e:
                print(e)
                video.length = None
            video.url = urljoin(current_app.config["VIDEO_PATH"], url)
        except Exception as e:
            return ReturnObj.get_response(ReturnEnum.UPLOAD_VIDEO.value,
                                          "请上传视频")
        try:
            file = request.files[form.logo.name]
            if not allowed_image_file(file.filename):
                return ReturnObj.get_response(
                    ReturnEnum.IMAGE_TYPE_ERROR.value,
                    "只允许上传png jpg jpeg gif格式")
            file_logo = secure_filename(file.filename)
            logo = change_filename(file_logo)
            file.save(os.path.join(current_app.config["LOGO_DIR"], logo))
            video.logo = urljoin(current_app.config["LOGO_PATH"], logo)
        except Exception as e:
            return ReturnObj.get_response(ReturnEnum.UPLOAD_VIDEO_LOGO.value,
                                          "请上传视频封面")
        db.session.add(video)
    with db.auto_commit():
        video = Video.query.filter(Video.name == form.name.data).order_by(
            Video.create_time.desc()).first()
        bangumi.episodes += 1
        bangumi.new_piece += 1
        if form.is_finish.data == 1:
            bangumi.is_finish = 1
        db.session.add(bangumi)
    with db.auto_commit():
        episode = Episode()
        episode.video_id = video.id
        episode.bangumi_id = bangumi.id
        episode.piece = bangumi.new_piece
        db.session.add(episode)
    write_oplog()
    return ReturnObj.get_response(ReturnEnum.SUCCESS.value, "success")
Пример #2
0
def add_video():
    form = VideoAddForm()
    # 每次刷新列表,动态加载最新的标签
    # form.tag_id.choices = [(v.id, v.name) for v in Tag.query.all()]
    form.validate_for_api()
    with db.auto_commit():
        video = Video()
        video.name = form.name.data
        try:
            file = request.files[form.url.name]
            if not allowed_video_file(file.filename):
                return ReturnObj.get_response(
                    ReturnEnum.VIDEO_TYPE_ERROR.value,
                    "只允许上传mp4 avi flv wmv格式")
            file_url = secure_filename(file.filename)
            url = change_filename(file_url)
            file.save(os.path.join(current_app.config["VIDEO_DIR"], url))
            try:
                video_clip = VideoFileClip(
                    os.path.join(current_app.config["VIDEO_DIR"], url))
                video.length = video_clip.duration
                video_clip.reader.close()
                video_clip.audio.reader.close_proc()
            except Exception as e:
                print(e)
                video.length = None
            video.url = urljoin(current_app.config["VIDEO_PATH"], url)
        except Exception as e:
            return ReturnObj.get_response(ReturnEnum.UPLOAD_VIDEO.value,
                                          "请上传视频")
        try:
            file = request.files[form.logo.name]
            if not allowed_image_file(file.filename):
                return ReturnObj.get_response(
                    ReturnEnum.IMAGE_TYPE_ERROR.value,
                    "只允许上传png jpg jpeg gif格式")
            file_logo = secure_filename(file.filename)
            logo = change_filename(file_logo)
            file.save(os.path.join(current_app.config["LOGO_DIR"], logo))
            video.logo = urljoin(current_app.config["LOGO_PATH"], logo)
        except Exception as e:
            return ReturnObj.get_response(ReturnEnum.UPLOAD_VIDEO_LOGO.value,
                                          "请上传视频封面")
        # 默认所属用户为pilipili番剧
        video.user_id = 6666
        video.info = form.info.data
        # 默认所属标签为连载动画
        video.tag_id = 18
        db.session.add(video)
    write_oplog()
    return ReturnObj.get_response(ReturnEnum.SUCCESS.value, "success")