rogerxavier commited on
Commit
cbc308a
1 Parent(s): 1d3689e

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +21 -14
api.py CHANGED
@@ -15,6 +15,7 @@ import time
15
  from bilibili_api import sync, video_uploader, Credential#bili上传部分
16
  import asyncio #保证后台任务的先后
17
  import json #对bili_meta_data进行dumps ->str和loads—>dict
 
18
 
19
 
20
  sessdata = os.getenv('sessdata')
@@ -105,32 +106,36 @@ async def execute_py_file(file_name: str,background_tasks: BackgroundTasks):
105
  #保证既要提交队列后返回给用户,又要先完成前面的video生成步骤再执行后面的submit函数,还有兼顾allow_submit的值
106
  @app.get("/execute_all_task")
107
  async def execute_all_task(background_tasks: BackgroundTasks, bili_meta_data,file_list: List[str] = Query(["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"]),
108
- cover_image: UploadFile = File(...), mp4_out_file: str = 'mp4_out/output.mp4',
109
- allow_submit: bool = False, cover_path: str = '/cover'
110
  ):
 
 
111
  print("获取到的用户bili_meta_data是:", bili_meta_data)
112
  #这里需要先转换bili_meta_data从str变成dict才能继续
113
  bili_meta_data = json.loads(bili_meta_data)
114
  print("获取到的file_list是",file_list)
115
  print("获取到的allow_submit是",allow_submit)
116
 
117
- background_tasks.add_task(process_tasks_and_upload, file_list, bili_meta_data, cover_image, cover_path,allow_submit,background_tasks)
118
  return {"message": "提交成功"}
119
 
120
- async def process_tasks_and_upload(file_list, bili_meta_data, cover_image, cover_path,allow_submit,background_tasks):
121
 
122
  tasks = []
123
  for file_name in file_list:
124
  tasks.append(file_executer(file_name))
125
 
126
  if allow_submit:
127
- cover_img = cover_image.file.read()
128
- cover_img_data = Image.open(io.BytesIO(cover_img)).convert("L").convert("RGB")
129
- cover_path_to_image = os.path.join(cover_path, "cover.jpg")
130
- cover_img_data.save(cover_path_to_image)
131
- print("cover_path_to_image是",cover_path_to_image)
132
- # background_tasks.add_task(upload_video, bili_meta_data, cover_path_to_image)
133
- tasks.append(await upload_video(bili_meta_data, cover_path_to_image))
 
 
134
 
135
 
136
 
@@ -158,16 +163,18 @@ def returnRandomSubscribeUrl(background_tasks: BackgroundTasks)->str:
158
 
159
 
160
 
161
- async def upload_video(meta:dict,cover_path_to_image:str):
162
  credential = Credential(sessdata=sessdata,
163
  bili_jct=bili_jct,
164
  buvid3=buvid3)
 
 
165
 
166
 
167
  page = video_uploader.VideoUploaderPage(path='mp4_out/output_video.mp4', title=meta['title'], description=meta['desc'])
168
- print("上传前的cover_path_to_image是",cover_path_to_image)
169
 
170
- uploader = video_uploader.VideoUploader([page], meta, credential, cover=cover_path_to_image)
171
 
172
  @uploader.on("__ALL__")
173
  async def ev(data):
 
15
  from bilibili_api import sync, video_uploader, Credential#bili上传部分
16
  import asyncio #保证后台任务的先后
17
  import json #对bili_meta_data进行dumps ->str和loads—>dict
18
+ import bilibili_api
19
 
20
 
21
  sessdata = os.getenv('sessdata')
 
106
  #保证既要提交队列后返回给用户,又要先完成前面的video生成步骤再执行后面的submit函数,还有兼顾allow_submit的值
107
  @app.get("/execute_all_task")
108
  async def execute_all_task(background_tasks: BackgroundTasks, bili_meta_data,file_list: List[str] = Query(["1removeMask", "2magiDialogCut", "3mergeDialogToVideo"]),
109
+ mp4_out_file: str = 'mp4_out/output.mp4',
110
+ allow_submit: bool = False, cover_url: str = ''
111
  ):
112
+ #cover_url因为本地上传有协程问题,所以采用cover_url然后转换为Picture类型即可->bilibili_api.utls.picture.Picture.from_url(str)->str
113
+
114
  print("获取到的用户bili_meta_data是:", bili_meta_data)
115
  #这里需要先转换bili_meta_data从str变成dict才能继续
116
  bili_meta_data = json.loads(bili_meta_data)
117
  print("获取到的file_list是",file_list)
118
  print("获取到的allow_submit是",allow_submit)
119
 
120
+ background_tasks.add_task(process_tasks_and_upload, file_list, bili_meta_data, cover_url,allow_submit,background_tasks)
121
  return {"message": "提交成功"}
122
 
123
+ async def process_tasks_and_upload(file_list, bili_meta_data, cover_url,allow_submit,background_tasks):
124
 
125
  tasks = []
126
  for file_name in file_list:
127
  tasks.append(file_executer(file_name))
128
 
129
  if allow_submit:
130
+ # cover_img = cover_image.file.read()
131
+ # cover_img_data = Image.open(io.BytesIO(cover_img)).convert("L").convert("RGB")
132
+ # cover_path_to_image = os.path.join(cover_path, "cover.jpg")
133
+ # cover_img_data.save(cover_path_to_image)
134
+ # print("cover_path_to_image是",cover_path_to_image)
135
+ # # background_tasks.add_task(upload_video, bili_meta_data, cover_path_to_image)
136
+ #因为采用cover_url上传,所以不需要读取内容然后保存本地->这样也会引起协程问题
137
+ print("开始对cover_url是",cover_path_to_image,"的提交进程进行加入后台任务")
138
+ tasks.append(await upload_video(bili_meta_data, cover_url))
139
 
140
 
141
 
 
163
 
164
 
165
 
166
+ async def upload_video(meta:dict,cover_url:str):
167
  credential = Credential(sessdata=sessdata,
168
  bili_jct=bili_jct,
169
  buvid3=buvid3)
170
+
171
+ cover = bilibili_api.utils.picture.Picture.from_url(cover_url)
172
 
173
 
174
  page = video_uploader.VideoUploaderPage(path='mp4_out/output_video.mp4', title=meta['title'], description=meta['desc'])
175
+ print("上传前的cover_url是",cover_url)
176
 
177
+ uploader = video_uploader.VideoUploader([page], meta, credential, cover=cover)
178
 
179
  @uploader.on("__ALL__")
180
  async def ev(data):