A

유튜브 API를 활용한 영상 업로드 및 활용

· 2012-09-05 (수) 15:52:52 · 6932 · 9
저는 ASP가 주종목이라 요번에 회사에서 외주개발자에게 php프로젝트 맡겼는데
몇몇 작업들은 저보고 하라고그래서 작업한 실소스입니다.
리눅스 php는 거의 초보수준이라 asp프로그래밍 로직에 대입해서 생각하면서
별것도 아닌 결과물을 쉽게 않게 처리했네요.
아래 게시물 중에 ffmpeg 관련된것도 같은 프로젝트 때문...유튜브야 뭐 소스 돌아다니는게 워낙 많아서
어렵지 않았지만, ffmpeg+nfs는 php리눅스 초보자로써는 참 골치아픈 작업이었네요.
제 소스들은 다 직접 사용해보고 올리는거라 믿음을 가지시고 참고하셔도 됩니다.

uccupload.php
<?php

$youtube_email = "유튜브 계정 이메일"; // Change this to your youtube sign in email.
$youtube_password = "패스워드"; // Change this to your youtube sign in password.
 
$postdata = "Email=".$youtube_email."&Passwd=".$youtube_password."&service=youtube&source=Example";
$curl = curl_init("https://www.google.com/youtube/accounts/ClientLogin");
curl_setopt($curl, CURLOPT_HEADER, "Content-Type:application/x-www-form-urlencoded");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
$response = curl_exec($curl);
curl_close($curl);
 
list($auth, $youtubeuser) = explode("\n", $response);
list($authlabel, $authvalue) = array_map("trim", explode("=", $auth));
list($youtubeuserlabel, $youtubeuservalue) = array_map("trim", explode("=", $youtubeuser));
 
' 제목값을 받아오는데 한글 깨져서 처리..그냥 이 페이지에서 입력받아도 됨.
 $_REQUEST['title'] = iconv("euc-kr", "utf-8", $_REQUEST['title']);
 
$youtube_video_title = $_REQUEST['title']; // This is the uploading video title.
$youtube_video_description = "설명"; // This is the uploading video description.
$youtube_video_category = "Entertainment"; // This is the uploading video category. 카테고리는 일일이 확인하면서 간추렸는데...카테고리만 자동으로 불러오는 API는 모르겠음.
$youtube_video_keywords = "키워드"; // This is the uploading video keywords.
 
$data = '<?xml version="1.0"?>
            <entry xmlns="http://www.w3.org/2005/Atom"
              xmlns:media="http://search.yahoo.com/mrss/"
              xmlns:yt="http://gdata.youtube.com/schemas/2007">
              <media:group>
                <media:title type="plain">'.$youtube_video_title.'</media:title>
                <media:description type="plain">'.$youtube_video_description.'</media:description>
                <media:category
                  scheme="http://gdata.youtube.com/schemas/2007/categories.cat">'.$youtube_video_category.'</media:category>
                <media:keywords>'.$youtube_video_keywords.'</media:keywords>
              </media:group>
            </entry>';
 
$key = "키값"; // Get your key here: http://code.google.com/apis/youtube/dashboard/.
 
$headers = array("Authorization: GoogleLogin auth=".$authvalue,
                 "GData-Version: 2",
                 "X-GData-Key: key=".$key,
                 "Content-length: ".strlen($data),
                 "Content-Type: application/atom+xml; charset=UTF-8");
 
$curl = curl_init("http://gdata.youtube.com/action/GetUploadToken");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_REFERER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
 
$response = simplexml_load_string(curl_exec($curl));
curl_close($curl);
?>
<?php
$nexturl = "http://경로/ucc_ret.php"; // This parameter specifies the URL to which YouTube will redirect the user's browser when the user uploads his video file.
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
//document.oncontextmenu=new Function('return false');
//document.ondragstart=new Function('return false');
//document.onselectstart=new Function('return false');
  function checkForFile() {
    if (document.getElementById('file').value) {
      return true;
    }
    document.getElementById('errMsg').style.display = '';
    document.getElementById('form_before').style.display='none';
    document.getElementById('form_next').style.display='block';
    return false;
  }
</script>
</head>
<body>
<div id="form_before">
<form action="<?php echo($response->url); ?>?nexturl=<?php echo(urlencode($nexturl)); ?>" method="post" enctype="multipart/form-data" onSubmit="return checkForFile();">
  파일 : <input id="file" type="file" name="file"/>
  <div id="errMsg" style="display:none;color:red">
    영상 파일을 첨부해주세요.
  </div><br>
  제목 : <? echo($youtube_video_title); ?><br>
  <input type="hidden" name="token" value="<?php echo($response->token); ?>"/>
  <input type="submit" value="유튜브로 전송" />
</form>
</div>
<br><br><br>
※ 업로드창이 자동으로 닫힐 때까지 창을 닫지 마세요.
</body>
</html>

------------------------------------------------------------------------------------------------------
ucc_ret.php

<?php
    $ucc_url = $_REQUEST['id'];
    $ucc_status = $_REQUEST['status'];
   
    if($ucc_status == "200"){
        ?>
        <script language="javascript">
        <!--
            //저는 부모창에다가 값 넘겨주는 식으로 처리함. 이 페이지에서 값 받은걸로 DB저장하든 뭘 하든 맘대로 하면됨.
            opener.document.neighborhood_from.bb_2.value='http://www.youtube.com/v/<? echo $ucc_url ?>';
            opener.document.getElementById('bb_2_div').innerHTML='http://www.youtube.com/v/<? echo $ucc_url ?>';
            window.close();
        //-->
        </script>
        <?php
    }else{
        ?>
        <script language="javascript">
        <!--
            alert("Fail");
            window.close();
        //-->
        </script>
<?php
    }
?>

------------------------------------------------------------------------------------------------------
그외에 리스트에서 유튜브 썸네일 불러올땐
http://i2.ytimg.com/vi/<?=str_replace('http://www.youtube.com/v/','',$row[bb_2])?>/default.jpg
이렇게 했습니다.
$row[bb_2]는 위에서 업로드하고 저장한 영상 주소 DB값입니다.
|

댓글 9개

오늘 월급날이라 기분 업되서 뜬금없이 게시물 몇개 올렸습니다. ㅎㅎ
멋지십니다.^^
2012-09-11 (화) 08:16:28
삭제는 어케 하는건가요?
삭제는 제 경우에는 유튜브에 제 계정으로 접속하면 거기에 관리페이지가 있는데 거기서 삭제해요. API를 통해서 삭제를 하는건 저도 아직 모르겠네요.
사용의 경우는 대부분 게시판 등에 글 등록할때 에디터 등으로 글 내용에 포함되어서 들어가는데, 유튜브 관리자에서 해당 영상을 삭제하면, 게시판 글 내용단에서도 플레이어 나오고 "삭제된 영상입니다" 이런식으로 표시가 됩니다.
감사합니다. 잘쓸께요~ㅎ
2012-09-13 (목) 10:27:17
음 참 괜춤한 방법이네요
좋은 정보 감사드려요 ^^
좋은 자료 감사합니다.
감사합니다. 잘쓰겠습니다.
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
13-02-18 조회 3,515
13-02-15 조회 3,922
13-02-12 조회 5,767
13-02-04 조회 4,074
13-01-30 조회 4,934
13-01-25 조회 3,680
13-01-18 조회 3,755
13-01-18 조회 5,488
13-01-18 조회 6,111
13-01-18 조회 3,778
13-01-18 조회 4,727
13-01-17 조회 4,053
13-01-17 조회 3,558
13-01-17 조회 3,617
13-01-17 조회 3,963
12-12-31 조회 3,515
12-12-28 조회 4,899
12-12-28 조회 5,958
12-12-28 조회 3,400
12-12-27 조회 3,557
12-12-27 조회 4,280
12-12-20 조회 5,196
12-12-18 조회 9,055
12-12-14 조회 4,887
12-12-10 조회 3,621
12-12-10 조회 3,091
12-12-01 조회 3,803
12-11-25 조회 7,275
12-11-22 조회 5,909
12-11-20 조회 2.4만
12-11-19 조회 4,308
12-11-13 조회 3,853
12-11-12 조회 3,862
12-10-23 조회 4,694
12-09-25 조회 4,330
12-09-05 조회 6,933
12-09-04 조회 7,490
12-09-04 조회 3,415
12-08-30 조회 3,374
12-08-30 조회 3,593
12-08-29 조회 3,329
12-08-29 조회 3,144
12-08-26 조회 3,452
12-08-18 조회 3,692
12-08-17 조회 3,976
12-08-16 조회 3,521
12-08-14 조회 3,176
12-08-13 조회 5,582
12-08-13 조회 3,337
12-08-11 조회 3,617
12-08-10 조회 3,188
12-08-07 조회 3,955
12-08-07 조회 3,187
12-08-06 조회 3,400
12-07-31 조회 3,168
12-07-30 조회 3,486
12-07-30 조회 3,120
12-07-29 조회 3,299
12-07-29 조회 3,050
12-07-27 조회 3,104
12-07-21 조회 2,977
12-07-19 조회 3,181
12-07-19 조회 2,767
12-07-17 조회 3,403
12-07-17 조회 3,025
12-07-17 조회 3,936
12-07-16 조회 2,968
12-07-15 조회 3,092
12-07-14 조회 3,141
12-07-13 조회 8,226
12-07-11 조회 3,374
12-07-10 조회 9,945
12-07-09 조회 3,152
12-07-08 조회 4,337
12-07-06 조회 3,622
12-07-05 조회 3,908
12-07-05 조회 3,325
12-07-05 조회 3,182
12-07-03 조회 3,221
12-07-03 조회 3,162
12-07-02 조회 3,198
12-07-02 조회 2,822
12-06-30 조회 3,428
12-06-26 조회 3,137
12-06-22 조회 3,178
12-06-22 조회 3,120
12-06-21 조회 3,100
12-06-21 조회 3,320
12-06-20 조회 3,488
12-06-20 조회 3,158
12-06-20 조회 3,064
12-06-18 조회 3,944
12-06-17 조회 3,377
12-06-15 조회 2,999
12-06-14 조회 2,885
12-06-13 조회 3,500
12-06-11 조회 3,320
12-06-08 조회 3,282
12-06-06 조회 3,138
12-06-06 조회 3,546