6

Chúng tôi đang sử dụng API phát trực tiếp trên YouTube cùng với Google API PHP Client và tôi không thể làm cách nào để sử dụng API đó (nhập trước) cơ bản thay vì tùy chỉnh một.Sử dụng chế độ nhập cơ bản khi sử dụng API phát trực tiếp trên YouTube hoặc tránh các tùy chỉnh trùng lặp

Tùy chỉnh là OK, nhưng vì một số lý do ngay cả khi bạn gọi cho chúng cùng tên, nó liên tục tạo bản sao cho mỗi luồng bạn tạo.

Vì vậy, câu hỏi của tôi là, làm cách nào để chúng tôi sử dụng tính năng nhập cơ bản hoặc có thể chọn tùy chỉnh mà không phải tạo tài khoản mới mỗi lần?

Ví dụ ở đây là ăn cơ bản bạn có thể chọn khi bạn thiết lập một dòng suối bằng tay trong tài khoản YouTube của bạn:

YouTube encoder

Các liên quan PHP mã:

// Create an object for the liveBroadcast resource's snippet. Specify values 
// for the snippet's title, scheduled start time, and scheduled end time. 
$broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet(); 
$broadcastSnippet->setTitle($this->title); 
$broadcastSnippet->setDescription($this->desc); 
$broadcastSnippet->setScheduledStartTime($this->start_time); 

// Create an object for the liveBroadcast resource's status, and set the 
// broadcast's status. 
$status = new Google_Service_YouTube_LiveBroadcastStatus(); 
$status->setPrivacyStatus($this->privacy_status); 

// Create the API request that inserts the liveBroadcast resource. 
$broadcastInsert = new Google_Service_YouTube_LiveBroadcast(); 
$broadcastInsert->setSnippet($broadcastSnippet); 
$broadcastInsert->setStatus($status); 
$broadcastInsert->setKind('youtube#liveBroadcast'); 

// Execute the request and return an object that contains information 
// about the new broadcast. 
$broadcastsResponse = $this->youtube->liveBroadcasts->insert('snippet,status', $broadcastInsert, array()); 

// Create an object for the liveStream resource's snippet. Specify a value 
// for the snippet's title. 
$streamSnippet = new Google_Service_YouTube_LiveStreamSnippet(); 
$streamSnippet->setTitle($this->stream_title); 

// Create an object for content distribution network details for the live 
// stream and specify the stream's format and ingestion type. 
$cdn = new Google_Service_YouTube_CdnSettings(); 
# TODO: Update the below `Format` method to use the new 'resolution' and 'frameRate' methods 
$cdn->setFormat($this->format); 
$cdn->setIngestionType('rtmp'); 

// Create the API request that inserts the liveStream resource. 
$streamInsert = new Google_Service_YouTube_LiveStream(); 
$streamInsert->setSnippet($streamSnippet); 
$streamInsert->setCdn($cdn); 
$streamInsert->setKind('youtube#liveStream'); 

// Execute the request and return an object that contains information 
// about the new stream. 
$streamsResponse = $this->youtube->liveStreams->insert('snippet,cdn', $streamInsert, array()); 

// Bind the broadcast to the live stream. 
$bindBroadcastResponse = $this->youtube->liveBroadcasts->bind(
    $broadcastsResponse['id'], 'id,contentDetails', 
    array(
     'streamId' => $streamsResponse['id'], 
    )); 

Trả lời

0

Ok, từ những gì tôi có thể nói không có cách nào để sử dụng nhập cơ bản nhưng tôi đã tìm hiểu cách sử dụng chế độ nhập tùy chỉnh hiện tại.

Bạn có thể tạo luồng qua mã nếu bạn thích hoặc tạo luồng theo cách thủ công trong giao diện YouTube.

Khi việc này được thực hiện, bạn sẽ cần tải luồng ID luồng bạn muốn liên kết với chương trình phát sóng mới mà bạn đã tạo; Tôi không thể nói một cách để tìm hiểu thông tin này thông qua giao diện YouTube để bạn có thể làm điều đó với API.

Bạn có thể sử dụng đoạn mã sau để có được một danh sách các kênh với list method:

// Execute an API request that lists the streams owned by the user who 
// authorized the request. 
$streamsResponse = $this->youtube->liveStreams->listLiveStreams('id,snippet', array(
    'mine' => 'true', 
)); 

$htmlBody .= "<h3>Live Streams</h3><ul>"; 
foreach ($streamsResponse['items'] as $streamItem) { 
    $htmlBody .= sprintf('<li>%s (%s)</li>', $streamItem['snippet']['title'], 
         $streamItem['id']); 
} 
$htmlBody .= '</ul>'; 

Lưu ý rằng đoạn code trên là một cuống; bạn có thể xem ví dụ đầy đủ ở phương pháp danh sách được liên kết ở trên; về cơ bản bạn sẽ vẫn cần thực hiện cuộc gọi đến Google_Client, Google_Service_YouTube và đảm bảo bạn có mã thông báo truy cập hợp lệ, v.v.

Khi bạn có id luồng mà bạn đã nhận được thông qua quá trình trên; sau đó bạn có thể làm một cái gì đó giống như dưới đây để sử dụng dòng cụ thể mà bạn muốn:

// Create an object for the liveBroadcast resource's snippet. Specify values 
// for the snippet's title, scheduled start time, and scheduled end time. 
$broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet(); 
$broadcastSnippet->setTitle($this->title); 
$broadcastSnippet->setDescription($this->desc); 
$broadcastSnippet->setScheduledStartTime($this->start_time); 

// Create an object for the liveBroadcast resource's status, and set the 
// broadcast's status. 
$status = new Google_Service_YouTube_LiveBroadcastStatus(); 
$status->setPrivacyStatus($this->privacy_status); 

// Create the API request that inserts the liveBroadcast resource. 
$broadcastInsert = new Google_Service_YouTube_LiveBroadcast(); 
$broadcastInsert->setSnippet($broadcastSnippet); 
$broadcastInsert->setStatus($status); 
$broadcastInsert->setKind('youtube#liveBroadcast'); 

// Execute the request and return an object that contains information 
// about the new broadcast. 
$broadcastsResponse = $this->youtube->liveBroadcasts->insert('snippet,status', $broadcastInsert, array()); 

// Bind the broadcast to the live stream. 
$bindBroadcastResponse = $this->youtube->liveBroadcasts->bind(
    $broadcastsResponse['id'], 'id,contentDetails', 
    array(
     'streamId' => 'stream_id_here', // <-- Insert your stream ID here 
    )); 

Một lần nữa, các mã trên là một cuống. Vì vậy, về cơ bản dòng dưới cùng là khi bạn có ID luồng bạn muốn sử dụng, bạn có thể xóa hoàn toàn mã tạo luồng và sau đó chỉ cần chuyển vào id luồng mà bạn đã có trong cuộc gọi đến bind phương pháp và bạn nên được tốt.

Hy vọng điều này sẽ giúp người khác.

2

tôi m không chắc chắn ý bạn là gì bởi "nhập cơ bản". Theo API, thuộc tính nhập chỉ có thể cài đặt là cdn.ingestionType chỉ hỗ trợ nhập RTMP tại thời điểm này.

Tương đương với cài đặt bộ mã hóa bạn thấy trong cổng web là giá trị cdn.format, cung cấp giao diện để chọn cặp độ phân giải bitrate cho Phát sóng trực tiếp của bạn. Thuộc tính này không còn được dùng nữa vào ngày 18 tháng 4 năm 2016 để có hai thuộc tính mới: cdn.frameRatecdn.resolution. Giá trị bitrate được liệt kê trong cổng web là tốc độ bit được đề xuất cho mỗi độ phân giải, được định cấu hình bởi bộ mã hóa của bạn, không phải API.

Cài đặt đúng định dạng cdn tùy chỉnh không được gây ra các đối tượng Dòng trực tiếp trùng lặp. Có thể có lỗi trong mã của bạn. Nếu bạn cảm thấy rằng đây là lỗi API, tôi khuyên bạn nên mở một vé cho Google here.

+0

Câu hỏi được cập nhật để hiển thị ví dụ về * nhập cơ bản *. – Brett

+0

@Brett đã cập nhật câu trả lời của tôi. – JAL

+0

Vâng, tôi biết về 'cdn.format' không được dùng nữa nhưng tiếc là 'Google API PHP Client' chưa thêm hỗ trợ cho cách mới để thực hiện nó nhưng AFAIK; ít nhất là trong nhánh V1. – Brett

Các vấn đề liên quan