Showing posts with label YouTube. Show all posts
Showing posts with label YouTube. Show all posts

Wednesday, 24 May 2017

How to download youtube video in PHP?

How to download youtube video in PHP?

Question: How to Get the YouTube Video Id?
Suppose we have following YouTube URL:
https://www.youtube.com/watch?v=iHp5veCNPVw
iHp5veCNPVw is YouTube Id.



Question: How to download YouTube using YouTube video Id?
$vid = 'iHp5veCNPVw'; //the youtube video ID
$vformat = 'video/mp4'; //video/mp4, video/webm, video/mp3 et.
$fileName = 'downloadvideos';//File name, downloadvideos.mp4
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=" . $vid), $info); //decode the data
$streams = $info['url_encoded_fmt_stream_map']; //the video's location info
$streams = explode(',', $streams);
foreach ($streams as $stream) {
    parse_str($stream, $data); //decode the stream
    if (stripos($data['type'], $vformat) !== false) { //We've found the right stream with the correct format
        $video = fopen($data['url'] . '&signature=' . @$data['sig'], 'r'); //the video
        $fullFileName = $fileName . '.' . str_replace('video/', '', $vformat);
        $file = fopen($fullFileName, 'w');
        stream_copy_to_stream($video, $file); //copy it to the file
        fclose($video);
        fclose($file);
        echo 'Downloaded Path:  ' . __DIR__ . '/' . $fullFileName;
        break;
    } else {
        echo "Unable to download this video.";
    }
}



Thursday, 1 December 2016

Difference between channel and playlist on youtube

Difference between channel and playlist on youtube

A channel is your identity on YouTube, like your name, your upload videos and comment as well.
OR
A channel  is the home page for an your account.

Question: What details on Available on home Channel page
--- Basic information like Your Name, Account type etc.
--- Your public videos which you have uploaded.
--- Listing of playlist.


Question: What we can do on on my Channel page
--- Update your information. --- upload videos.
--- customize the color theme of your channel.
--- Others can subscribe your channel.
Question: Sample of Channel?
https://www.youtube.com/channel/UCFPS0qKgr-GbSRAppOkdpUA





Question: What is playlist?
A playlist is a collection of videos you may saved.


Question: List of Playlist (After Login on youtube.com with your gmail.com account)
https://www.youtube.com/view_all_playlists


Create a New Playlist (After Login on youtube)
https://www.youtube.com/view_all_playlists
Click on "New Playlist", an popup will prompt, just add the your playlist name


Question: How to add videos in playlist?
Go to playlist page (Like https://www.youtube.com/playlist?list=PLREKFLRZTg5xJJHmLiKZ0VBTKpubGXksb).
Click on "Add Videos" (Middle Right side).
An Popup will prompt.
Just add the YouTube video URL (Any video which you like) and follow Steps.


Question: You can do following things with your playlist
--- Make the playlist as public/private.
--- Add notes to each videos.
--- Re-ordering the videos.
--- AutoPlay on/off for videos.
--- Share your playlist on Fb.
--- Embed your playlist videos in your website/blog.
Question: Need Playlist Sample URL
https://www.youtube.com/playlist?list=PLREKFLRZTg5xJJHmLiKZ0VBTKpubGXksb



Question: Can we earn money with playlist? No, You can't earn money on other's video (Uploaded by other).
You can earn money with your own videos (uploaded by you)

Friday, 16 September 2016

How to Dynamically change Youtube Player video

How to Dynamically change Youtube Player videoID



<script src="//www.youtube.com/iframe_api" type="text/javascript"></script>
<script type="text/javascript">
    /** List of youtube videos */
    var videos = new Array('tGiJSnwBwDQ', 'NjPCjCCjMH4', 'ioWkx6WRH2I')

    var currentId = -1;
    /** Create player object **/
    var player;
    function onYouTubeIframeAPIReady()
    {
        player = new YT.Player('youtubePlayerId', {events: {onReady: onPlayerReady, onStateChange: onPlayerStateChange}})
    }
    function onPlayerReady(event) {
        /** Mute the video **/
        player.mute();
        player.setVolume(0);
        /** Mute the video **/
        //player.playVideo();
    }
    function onPlayerStateChange(event) {
        /* If video stop */
        if (event.data === 0) {
            currentId++;
            if (videos.length <= (currentId + 1)) {
                currentId = 0;
            }
            player.loadVideoById(videos[currentId]);
        }
        /* If video stop */
    }
</script>
<iframe frameborder="0" height="350" id="youtubePlayerId" src="//www.youtube.com/embed/0Bmhjf0rKe8?rel=0&amp;modestbranding=1&amp;autoplay=0&amp;enablejsapi=1" type="text/html" width="390"></iframe>

Thursday, 11 August 2016

How to Convert Youtube Data API V3 video duration format to seconds in PHP?

How to Convert Youtube Data API V3 video duration format to seconds in PHP?

We get the youTube Video duration in below way from Youtube V3 API.
 "contentDetails": {
    "duration": "PT15S",
    "dimension": "2d",
    "definition": "sd",
    "caption": "false",
    "licensedContent": true,
    "projection": "rectangular"
   },

Now, We need to convert the duration to number of seconds.
Following are Simple PHP Function which convert the API V3 video duration format to number of seconds.
 function duration($ytDuration) {
    $di = new DateInterval($ytDuration);
    
    $totalSec = 0;
    if ($di->h > 0) {
      $totalSec+=$di->h*3600;
    }
    if ($di->i > 0) {
      $totalSec+=$di->i*60;
    }
    $totalSec+=$di->s;
    
    return $totalSec;
  }
$youtubeTime = 'PT15S';
 echo duration($youtubeTime);//15
 
 
$youtubeTime = 'PT2M55S'; 
echo duration($youtubeTime);//175

$youtubeTime = 'PT1H35M23S'; 
echo duration($youtubeTime);//5723

Thursday, 21 July 2016

How to remove the YouTube Logo from the Video Player?

How to remove the YouTube Logo from the Video Player?

Question: How to remove the YouTube Logo from the Video Player?
<iframe height="300" src="//www.youtube.com/embed/myaqHeNEO04?modestbranding=1" width="400"></iframe>

Demo




Question: How to autoplay the youtube video when embed in website?
<iframe height="300" src="//www.youtube.com/embed/myaqHeNEO04?modestbranding=1&amp;autoplay=1" width="400"></iframe>

Demo




Question: How to Stop autoplay the youtube video when embed in website?
<iframe height="300" src="//www.youtube.com/embed/myaqHeNEO04?modestbranding=1&amp;autoplay=0" width="400"></iframe>

Demo




Question: How to remove related videos from the end of an embedded youtube video?
<iframe height="300" src="//www.youtube.com/embed/myaqHeNEO04?modestbranding=1&amp;autoplay=0&amp;rel=0" width="400"></iframe>

Demo




Wednesday, 20 July 2016

How to embed muted youtube video with autoplay

How to embed muted youtube video with autoplay

Question:How to Embed muted youtube video with autoplay?
<iframe allowfullscreen="" frameborder="0" height="300px" id="ytplayer" src="https://www.youtube-nocookie.com/embed/2gcfHpGpsOw?rel=0&amp;enablejsapi=1&amp;autoplay=1&amp;controls=1&amp;showinfo=0&amp;loop=1&amp;iv_load_policy=3" type="text/html" width="400px"></iframe>
<script>
var player;

function onYouTubeIframeAPIReady() {
    player = new YT.Player('ytplayer', {
        events: {
            'onReady': onPlayerReady
        }
    });
}

function onPlayerReady(event) {
    player.mute();
    player.playVideo();
}    
    </script>


Question:How to Embed muted youtube video without autoplay?
<iframe allowfullscreen="" frameborder="0" height="300px" id="ytplayer" src="https://www.youtube-nocookie.com/embed/2gcfHpGpsOw?rel=0&amp;enablejsapi=1&amp;autoplay=0&amp;controls=1&amp;showinfo=0&amp;loop=1&amp;iv_load_policy=3" type="text/html" width="400px"></iframe>
<script>
var player;

function onYouTubeIframeAPIReady() {
    player = new YT.Player('ytplayer', {
        events: {
            'onReady': onPlayerReady
        }
    });
}

function onPlayerReady(event) {
    player.mute();
    player.playVideo();
}    
    </script>

Demo