gpt4 book ai didi

python - 在 python 上按路径上传视频文件不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:52 25 4
gpt4 key购买 nike

我尝试使用python上传视频文件,问题是即使我写了文件路径,系统也找不到该文件。我的代码是这样的:

import os
import requests

#step 1
host = 'https://blablabla.com'

test = {
"upload_phase" : "start",
"file_size" : 1063565
}

params = {
"access_token":my_access_token,
"fields":"video_id, start_offset, end_offset, upload_session_id",
}

vids = requests.post(host, params=params, data=test)
vids = vids.json()

try:
video_id= vids["video_id"],
start_offset= vids["start_offset"],
end_offset= vids["end_offset"],
upload_session_id= vids["upload_session_id"]
except:
pass

print(vids)

###############################################################################
#step 2
###############################################################################
test = {
"upload_phase" : "transfer",
"start_offset" : start_offset,
"upload_session_id": upload_session_id,
"video_file_chunk": os.path.realpath('/home/def/Videos/test.mp4')
}

params = {
"access_token":my_access_token,
"fields":"start_offset, end_offset",
}

vids = requests.post(host, params=params, data=test)
vids = vids.json()

try:
start_offset= vids["start_offset"],
end_offset= vids["end_offset"]
except:
pass

print(vids)

我尝试了很多方法,比如 os.path.abspath、os.path、os.path.dirname、os.path.basename、os.path.isfile、os.path.isabs、os.path。 isdir 它仍然不起作用。即使我给出 import os.path 或 import os

最佳答案

在您的代码中,您只需将文件的路径作为字符串发送到服务器,而不是文件本身。你应该尝试这样的事情:

my_file = {'file_to_upload': open(os.path.realpath('/home/def/Videos/test.mp4'),'rb')} 
# You should replace 'file_to_upload' with the name server actually expect to receive
# If you don't know what server expect to get, check browser's devconsole while uploading file manually
vids = requests.post(host, params=params, files=my_file)

另请注意,您可能需要使用 requests.Session() 才能处理 cookie、访问 token ...

关于python - 在 python 上按路径上传视频文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41604347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com