Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INSTALLATION (Windows)
- 1. Install streamlink: https://streamlink.github.io/install.html#windows-binaries
- 2. Install PHP 7 and make sure .php files are opened by php.exe. If you don't know what you're doing, XAMPP should install everything properly: https://www.apachefriends.org/download.html
- 3. Copy the code below into a file named whatever.php in notepad. Adjust the first two lines ($dir and $format) to specify the directory to save files and the default format.
- USAGE
- 4. Run whatever.php, paste the video URL in the command line that opens and press enter. It will start recording to the directory you specified.
- <?php
- $dir = 'C:/Users/K/Videos'; # directory to save video
- $format = '720p'; # default format
- echo 'Enter URL: ';
- $url = trim(fgets(STDIN));
- $args = explode(' ', $url);
- $url = explode('&', $args[0])[0]; # Remove stuff after &
- /* There are a few arguments that you can optionally add after the URL (no hyphens):
- 480/720/1080 : Choose a non-default format.
- c (current) : Instead of recording from the beginning of the stream, record from the live time.
- p (play) : Open the video in VLC.
- q (quick) : Use an ugly filename instead of spending a second fetching the meta info.
- For example, you could type:
- https://www.youtube.com/watch?v=sEYzlLfLn-Y 1080 q p
- To record that video in 1080p, play it in VLC and use an ugly filename.
- */
- # If the URL is a /channel/.../live style URL, get the video URL.
- if(strpos($url, '/live') !== false) {
- $page = file_get_contents($url);
- preg_match('!"video_id":"(.*?)"!', $page, $matches);
- if(empty($matches[1])) {
- exit("Could not get real video URL.\n");
- }
- $url = 'https://www.youtube.com/watch?v=' . $matches[1];
- }
- # Format: 480, 720 or 1080
- foreach(array('480', '720', '1080') as $option) {
- if(in_array($option, $args)) {
- $format = $option . 'p';
- }
- }
- # Play in VLC only if "play" or "p" is present in the arguments
- $play = 'output';
- if(in_array('play', $args) || in_array('p', $args)) {
- $play = 'record';
- }
- # Start from the beginning of the stream by default, unless "current" or "c" is present
- $hls = '--hls-live-restart';
- if(in_array('current', $args) || in_array('c', $args)) {
- $hls = '';
- }
- # Get video title and author.
- function get_meta_info($url) {
- # Alternative: https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=lUOhCtXPN40&format=json
- $info = json_decode(file_get_contents('https://noembed.com/embed?url=' . urlencode($url)), true);
- $return = array();
- if( ! empty($info['title'])) {
- $return['title'] = $info['title'];
- $return['author'] = $info['author_name'];
- }
- else if(isset($info['error']) && $info['error'] == '401 Unauthorized') {
- # Embedding disabled. Fetch the info through a slightly slower method.
- $page = file_get_contents($url);
- preg_match('!"title":"(.*?)"!', $page, $matches);
- if( ! empty($matches[1])) {
- $return['title'] = stripslashes($matches[1]);
- preg_match('!"author":"(.*?)"!', $page, $matches);
- $return['author'] = stripslashes($matches[1]);
- }
- }
- return $return;
- }
- # Fetch full title for filename if "quick" and "q" are not present
- $code = explode('=', $url)[1];
- $title = date('Y-m-d H.i.s') . ' ' . $code;
- if( ! in_array('quick', $args) && ! in_array('q', $args)) {
- $info = get_meta_info($url);
- if(empty($info)) {
- echo "Failed to fetch info.\n";
- } else {
- echo "Retrieved meta info.\n";
- $video_title = $info['title'];
- $video_author = $info['author'];
- $title = date('Y-m-d') . ' - ' . $video_author . ' - ' . $video_title . ' - ' . $code;
- # Replace characters not allowed in filenames
- $find = array(
- '/',
- '<',
- '>',
- ':',
- '\\',
- '|',
- '?',
- '*'
- );
- $replace = array(
- '/',
- '<',
- '>',
- ':',
- '/',
- '|',
- '?',
- '*'
- );
- $title = str_replace($find, $replace, $title);
- if(file_exists($dir . '/' . $title . '.mp4')) {
- $title .= ' (' . date('H.i.s') . ')';
- }
- }
- }
- while(true) {
- echo "Attempting to start recording.\n";
- $response = exec('streamlink ' . $url . ' ' . $format . ' ' . $hls . ' --' . $play . ' "' . $dir . '/' . $title . '.mp4"');
- # If stream hasn't started, sleep until its scheduled time. After that, reattempt every 10 seconds.
- if(strpos($response, 'error: No playable streams found') !== false) {
- if( ! isset($start_time)) {
- $json = json_decode(file_get_contents('https://www.youtube.com/heartbeat?video_id=' . $code), true);
- if(isset($json['liveStreamability']['liveStreamabilityRenderer']['offlineSlate']['liveStreamOfflineSlateRenderer']['scheduledStartTime'])) {
- $start_time = (int) $json['liveStreamability']['liveStreamabilityRenderer']['offlineSlate']['liveStreamOfflineSlateRenderer']['scheduledStartTime'];
- } else {
- echo "Start timestamp not found.\n";
- break;
- }
- }
- if(time() > $start_time) {
- $start_time = time() + 10;
- }
- $remaining = $start_time - time();
- echo "Waiting " . $remaining . " seconds...\n";
- time_sleep_until($start_time);
- }
- else {
- # Stream finished.
- break;
- }
- }
- sleep(5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement