답변 1개
회원 동영상 업로드 -> YouTube API 이용하여 채널에 업로드 하면 가능할거 같습니다.
참고 1 : https://kminito.tistory.com/5#google_vignette
참고 2 : PHP 이용시
composer require google/apiclient
require_once 'vendor/autoload.php';
function getClient() { $client = new Google_Client(); $client->setApplicationName("YouTube Upload App"); $client->setScopes([ 'https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube' ]); $client->setAuthConfig('path/to/your/client_secret.json'); $client->setAccessType('offline');
// Load previously authorized credentials from a file. $credentialsPath = 'path/to/your/credentials.json'; if (file_exists($credentialsPath)) { $accessToken = json_decode(file_get_contents($credentialsPath), true); $client->setAccessToken($accessToken); }
// If there is no previous token or it's expired. if ($client->isAccessTokenExpired()) { // Refresh the token if possible, else fetch a new one. if ($client->getRefreshToken()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); } else { // Request authorization from the user. $authUrl = $client->createAuthUrl(); printf("Open the following link in your browser:\n%s\n", $authUrl); print 'Enter verification code: '; $authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token. $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); $client->setAccessToken($accessToken);
// Save the token to a file. if (!file_exists(dirname($credentialsPath))) { mkdir(dirname($credentialsPath), 0700, true); } file_put_contents($credentialsPath, json_encode($client->getAccessToken())); } } return $client; }
function uploadVideo($videoPath, $title, $description, $tags, $category = 22) { try { $client = getClient(); $youtube = new Google_Service_YouTube($client);
$snippet = new Google_Service_YouTube_VideoSnippet(); $snippet->setTitle($title); $snippet->setDescription($description); $snippet->setTags($tags); $snippet->setCategoryId($category);
$status = new Google_Service_YouTube_VideoStatus(); $status->setPrivacyStatus("private"); // or "public" or "unlisted"
$video = new Google_Service_YouTube_Video(); $video->setSnippet($snippet); $video->setStatus($status);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true); $insertRequest = $youtube->videos->insert("status,snippet", $video); $media = new Google_Http_MediaFileUpload( $client, $insertRequest, 'video/*', null, true, $chunkSizeBytes ); $media->setFileSize(filesize($videoPath));
$status = false; $handle = fopen($videoPath, "rb"); while (!$status && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $status = $media->nextChunk($chunk); } fclose($handle);
$client->setDefer(false);
if ($status) { echo "Video uploaded successfully. Video ID: " . $status['id']; }
return $status; } catch (Google_Service_Exception $e) { echo "An error occurred: " . $e->getMessage(); } catch (Google_Exception $e) { echo "An error occurred: " . $e->getMessage(); } }
// 사용 예시 $videoPath = 'path/to/your/video.mp4'; $title = '업로드 테스트 동영상'; $description = '이 동영상은 API를 통해 업로드되었습니다.'; $tags = ['테스트', 'API', '업로드'];
uploadVideo($videoPath, $title, $description, $tags);
답변에 대한 댓글 1개
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인