代码 324 无效视频和状态失败?
Code 324 Not Valid Video and Status failed?
我正在尝试通过 Twitter API 从我的网站上传视频。我为大文件抓取了他们的 github 库代码的异步上传文件。我正在分块上传数据。这是代码:
(请注意,出于测试目的,我使用静态文件大小和块肯定会感谢动态方法建议)
MEDIA_ENDPOINT_URL = 'https://upload.twitter.com/1.1/media/upload.json'
POST_TWEET_URL = 'https://api.twitter.com/1.1/statuses/update.json'
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_TOKEN = 'xxx-xxx'
ACCESS_TOKEN_SECRET = 'xxx'
VIDEO_FILENAME = request.FILES['video']
VIDEO_SIZE = 59467
oauth = OAuth1(CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=ACCESS_TOKEN,
resource_owner_secret=ACCESS_TOKEN_SECRET)
request_data = {
'command': 'INIT',
'media_type': 'video/mp4',
'total_bytes': VIDEO_SIZE,
'media_category': 'tweet_video'
}
req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, auth=oauth)
print req
media_id = req.json()['media_id']
print('Media ID: %s' % str(media_id))
segment_id = 0
bytes_sent = 0
vid_file = VIDEO_FILENAME
while bytes_sent < VIDEO_SIZE:
chunk = vid_file.read(59467)
print('APPEND')
request_data = {
'command': 'APPEND',
'media_id': media_id,
'segment_index': segment_id
}
files = {
'media': chunk
}
req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, files=files, auth=oauth)
if req.status_code < 200 or req.status_code > 299:
print(req.status_code)
print(req.text)
segment_id = segment_id + 1
bytes_sent = vid_file.tell()
print('%s of %s bytes uploaded' % (str(bytes_sent), str(VIDEO_SIZE)))
print('Upload chunks complete.')
request_data = {
'command': 'FINALIZE',
'media_id': media_id,
'media_category': 'tweet_video'
}
req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, auth=oauth)
print(req.json())
processing_info = req.json().get('processing_info', None)
print(req.status_code)
time.sleep(5)
request_data = {
'status': 'I just uploaded a video with the @TwitterAPI.',
'media_ids': req.json()['media_id_string'],
'media_category': 'tweet_video'
}
req = requests.post(url=POST_TWEET_URL, data=request_data, auth=oauth)
print(req.json())
context = {
'r': req
}
return render_to_response('dashboard/manage_content/display.html', context)
我遇到了以下错误:
{"errors":[{"code":324,"message":"Not valid video"}]}
I am uploading a mp4 file of 1.6 mb size. Please let me know if you need any more info.
解决方案是使用文件的实际大小而不是文件的一部分,并确保使用函数动态获取大小。即使所有块都已上传,静态大小也不起作用。
我正在尝试通过 Twitter API 从我的网站上传视频。我为大文件抓取了他们的 github 库代码的异步上传文件。我正在分块上传数据。这是代码: (请注意,出于测试目的,我使用静态文件大小和块肯定会感谢动态方法建议)
MEDIA_ENDPOINT_URL = 'https://upload.twitter.com/1.1/media/upload.json'
POST_TWEET_URL = 'https://api.twitter.com/1.1/statuses/update.json'
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_TOKEN = 'xxx-xxx'
ACCESS_TOKEN_SECRET = 'xxx'
VIDEO_FILENAME = request.FILES['video']
VIDEO_SIZE = 59467
oauth = OAuth1(CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=ACCESS_TOKEN,
resource_owner_secret=ACCESS_TOKEN_SECRET)
request_data = {
'command': 'INIT',
'media_type': 'video/mp4',
'total_bytes': VIDEO_SIZE,
'media_category': 'tweet_video'
}
req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, auth=oauth)
print req
media_id = req.json()['media_id']
print('Media ID: %s' % str(media_id))
segment_id = 0
bytes_sent = 0
vid_file = VIDEO_FILENAME
while bytes_sent < VIDEO_SIZE:
chunk = vid_file.read(59467)
print('APPEND')
request_data = {
'command': 'APPEND',
'media_id': media_id,
'segment_index': segment_id
}
files = {
'media': chunk
}
req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, files=files, auth=oauth)
if req.status_code < 200 or req.status_code > 299:
print(req.status_code)
print(req.text)
segment_id = segment_id + 1
bytes_sent = vid_file.tell()
print('%s of %s bytes uploaded' % (str(bytes_sent), str(VIDEO_SIZE)))
print('Upload chunks complete.')
request_data = {
'command': 'FINALIZE',
'media_id': media_id,
'media_category': 'tweet_video'
}
req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, auth=oauth)
print(req.json())
processing_info = req.json().get('processing_info', None)
print(req.status_code)
time.sleep(5)
request_data = {
'status': 'I just uploaded a video with the @TwitterAPI.',
'media_ids': req.json()['media_id_string'],
'media_category': 'tweet_video'
}
req = requests.post(url=POST_TWEET_URL, data=request_data, auth=oauth)
print(req.json())
context = {
'r': req
}
return render_to_response('dashboard/manage_content/display.html', context)
我遇到了以下错误:
{"errors":[{"code":324,"message":"Not valid video"}]} I am uploading a mp4 file of 1.6 mb size. Please let me know if you need any more info.
解决方案是使用文件的实际大小而不是文件的一部分,并确保使用函数动态获取大小。即使所有块都已上传,静态大小也不起作用。