如何通过 API 从 CDN URL 上传视频作为 Facebook Reel,而不需要下载到本地?

问题描述 投票:0回答:1

我正在开发一个 Python 脚本,该脚本使用 Facebook Graph API 将视频作为 Reels 直接上传到 Facebook 页面。我的视频托管在 CDN 上,我想将它们直接上传到 Facebook,而不将它们下载到我的本地服务器。

我目前正在使用 Facebook 的 video_reels 端点将视频作为卷轴上传。对于本地文件,通过以二进制形式上传视频,该过程可以完美运行。但是,当视频托管在 CDN 上时,我希望避免在本地下载视频,而是直接从 CDN URL 上传。

这是我当前代码的简化版本:

import requests

# Facebook API parameters
page_access_token = 'your_page_access_token'
page_id = 'your_page_id'
api_version = 'v20.0'
video_url = 'https://cdn.example.com/video.mp4'  # CDN URL of the video
video_description = "My awesome reel"
video_title = "Reel Title"
thumbnail_path = 'thumb.jpg'


# Step 1: Initialize the upload session for reels
def initialize_upload_reel():
    url = f"https://graph.facebook.com/{api_version}/{page_id}/video_reels"
    payload = {
        'upload_phase': 'start',
        'access_token': page_access_token
    }
    
    response = requests.post(url, data=payload)
    if response.status_code == 200:
        data = response.json()
        video_id = data['video_id']
        print(f"Upload session started. Video ID: {video_id}")
        return video_id
    else:
        raise Exception(f"Error initializing reel upload: {response.text}")

# Step 2: Upload the reel video file
def process_upload_reel(video_id, file_size):
    url = f"https://rupload.facebook.com/video-upload/v20.0/{video_id}"

    headers = {
        'Authorization': f'OAuth {page_access_token}',
        'Content-Type': 'application/octet-stream',
        'offset': '0',
        'file_size': str(file_size)
    }

    with open(video_url, 'rb') as file_data:
        response = requests.post(url, headers=headers, data=file_data)
    
    if response.status_code == 200:
        print("Reel Uploaded")
    else:
        raise Exception(f"Error uploading reel video: {response.text}")

def publish_reel(video_id, description, publish_time=None, published=True):
    url = f"https://graph.facebook.com/{api_version}/{page_id}/video_reels"
    thumb_file = {'thumb': open(thumbnail_url, 'rb')}
    
    payload = {
        'access_token': page_access_token,
        'video_id': video_id,
        'upload_phase': 'finish',
        'title': title_entry.get(),  # Ensure title is passed
        'description': description_entry.get()  # Ensure description is passed
    }

    if publish_time:
        payload['video_state'] = 'SCHEDULED'
        payload['scheduled_publish_time'] = publish_time
    else:
        payload['video_state'] = 'PUBLISHED' if published else 'DRAFT'

    response = requests.post(url, data=payload, files=thumb_file)
    thumb_file['thumb'].close()

    if response.status_code == 200:
        check_video_status(video_id)
        
    else:
        raise Exception(f"Error publishing reel: {response.text}")

问题: 当我尝试使用 CDN URL 上传时,收到以下错误消息:

{
  "error": {
    "message": "There was a problem uploading your video file. Please try again with another file.",
    "type": "OAuthException",
    "code": 6000,
    "error_subcode": 1363130,
    "error_user_title": "Video Upload Is Missing",
    "error_user_msg": "The video was not uploaded.",
    "fbtrace_id": "Ai3SicB2QxOVA-ZpIQu7cjT"
  }
}

Facebook 的 Reels API 似乎不支持使用 file_url 参数直接从 CDN URL 上传视频,或者我可能在实现中遗漏了一些东西。

问题: 是否可以通过 Graph API 将视频直接从 CDN URL 上传到 Facebook Reels,而无需先在本地下载视频?

如果是,我应该如何构建我的 API 请求来实现这一目标?

如果不是,在这种情况下处理 CDN 托管视频的推荐方法是什么(例如避免将视频下载到服务器)? 任何帮助或指导将不胜感激!

python facebook-graph-api cdn video-upload facebook-graph-api-v2.8
1个回答
0
投票

支持远程上传。我试图这样做,但使用 koltin for Android 应用程序时,由于视频的分辨率,我遇到了问题。它必须有一个纵横比

9×16

因此请确保您尝试上传的视频满足这些要求。

https://developers.facebook.com/docs/video-api/guides/reels-publishing/

© www.soinside.com 2019 - 2024. All rights reserved.