Subversion Repositories yt_downloader

Compare Revisions

No changes between revisions

Regard whitespace Rev 1 → Rev HEAD

/trunk/LICENSE
0,0 → 1,202
 
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
 
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
 
1. Definitions.
 
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
 
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
 
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
 
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
 
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
 
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
 
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
 
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
 
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
 
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
 
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
 
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
 
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
 
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
 
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
 
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
 
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
 
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
 
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
 
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
 
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
 
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
 
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
 
END OF TERMS AND CONDITIONS
 
APPENDIX: How to apply the Apache License to your work.
 
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
 
Copyright 2018 Daniel Marschall, ViaThinkSoft
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
/trunk/README.md
0,0 → 1,113
 
# ViaThinkSoft YouTube Downloader 2.4.1
 
YouTube Downloader is a tool for Linux. It is a wrapper for youtube-dl (or any compatible forks) and offers several additional functionalities.
 
Special features:
- Downloading of all videos of a channel or a playlist.
- Automatic searching inside channels or globally (whole YouTube)
- You can download videos and audio files.
- YouTube-IDs can be automatically written in the ID tag of downloaded mp3 files.
- An automatically managed list of already downloaded videos allows you to move away from the downloaded files without the risk of downloading the already downloaded files again.
- An automatically managed list of failed downloads will avoid that a video, which is not available anymore, is tried to be downloaded too many times.
- Creation of SFV and/or MD5 checksum files.
- The tool is fully CLI and is optimized for cronjobs.
 
## Syntax
 
./ytdwn [-t|--type v:[ext]|a:[ext]] Type video or audio and preferred output type, e.g. 'a:mp3' (default v:)
[-o|--outputDir <dir>] Default current working directory
[-a|--alreadyDownloaded <file>]
[-f|--failList <file> <treshold>] This file logs failures.
[-F|--failTreshold <num>] Don't download if failure (-f) treshold is reached. (Default: 3)
[-V|--version] Shows the version
[-v|--verbose] Displays verbose information to STDOUT
[-h|--help] Shows the help page.
[-N|--no-mp3-tagtransfer] Disables transfer of video ID to MP3 ID tag.
This feature requires the package "id3v2".
[-H|--checksumMode] Which checksum files shall be written for new files.
Must be 'None', 'MD5', 'SFV', or 'MD5,SFV'.
[-T|--default-template <t>] Sets default filename template.
(Default: '%(title)s-%(id)s.%(ext)s')
[-X|--extra-args <args>] Additional arguments passed through youtube-dl. (Default "-ic")
[-A|--api-key <file|key>] Specifies the API key, or a file containing the API key
(Default: ~/.yt_api_key)
[--cookies=<file>] A netscape compatible cookie file (for age restricted videos
(Default: ~/.yt_cookkies)
[-C|--resultcache <file>] Allows video results to be cached in this file;
only for playlists or channels.
[-O|--create-outputdir] Allows creation of the output directories, recursively.
[--downloader=exename] Binary file name of a youtube-dl compatible tool.
Currently supported/tested: youtube-dl, youtube-dlc, yt-dlp.
[--]
<resource> [<resource> ...]
 
For all paths (outputDir, alreadyDownloaded, apikey, failList and resultcache), you can use the
term '[listname]' which will be replaced by the basename of the current list file (without file extension).
For example you can do following:
 
./ytdwn -o 'downloads/[listname]' -- list:*.list
 
If no list file is processed, it will be replaced with nothing.
 
The "alreadyDownloaded" argument contains a file which will be managed by ytdwn.
It will contain all video IDs which have been downloaded. This allows you to
move away the already downloaded files, and ytdwn will not download them again.
 
Examples for type:
- `v:` = best video quality
- `a:` = best audio only
- `a:mp3` = audio only, mp3
- Valid audio formats according to "man youtube-dl" are
"best", "aac", "flac", "mp3", "m4a", "opus", "vorbis", or "wav"; "best" by default
 
A `<resource>` can be one of the following:
 
vid:<video ID>
vurl:<youtube video URL>
pid:<playlist ID>
purl:<playlist URL>
cid:<channel id>
cname:<channel name>
curl:<channel or username URL>
list:<file with resource entries> (comments can be #)
search:<searchterm>
 
For channels (`cid`, `cname`, `curl`) you can also perform a search to filter the results.
This can be done like this: `cname:[search="Elvis Presley"]channel_1234`
For the search option, following parameters are possible: `search:[order=date][maxresults=50]"Elvis Presley"`
Acceptable order values are: `date`, `rating`, `relevance`, `title`, `videoCount`, `viewCount`
Default values are `order=relevance` and `maxresults=10`
Use `maxresults=-1` to download everything which matches the searchterm.
 
## Requirements
- PHP CLI
- A YouTube API key (can be obtained here: https://console.developers.google.com/apis/credentials )
- If you want to extract audio, you need additionally: ffmpeg or avconv and ffprobe or avprobe.
- Optional: package "id3v2" to allow the YouTube video id to be transferred to the MP3 ID tag
 
## Age restricted videos how-to
 
To download age restricted videos, you need to supply cookies from a browser that has been logged in to YouTube.
 
**Here is a method how to do this:**
 
(1) Download and install this Chrome extension:
https://chrome.google.com/webstore/detail/cookiestxt/njabckikapfpffapmjgojcnbfjonfjfg/related
 
(2) If you cannot see the Cookie-Button at the right top, then click the plugin button, and then enable "pinning" for the plugin "cookie.txt".
 
(3) Login to YouTube at the same network where the YouTube Downloader will run.
Press the cookie plugin button and copy the contents to a file called cookies.txt
 
(4) Edit cookies.txt and add following comment at the very top: `# HTTP Cookie File`
 
(5) Rename and move the file to **~/.yt_cookies**
OR
Add the argument `--cookies=cookies.txt` to ytdwn.
 
## License
 
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
/trunk/checksum_functions.inc.phps
0,0 → 1,121
<?php
 
// ViaThinkSoft YouTube Downloader Util 2.3
// Revision: 2022-02-07
// Author: Daniel Marschall <www.daniel-marschall.de>
// Licensed under the terms of the Apache 2.0 License
 
function cs_get_checksumfilename($file, $type='sfv') {
$dir = dirname($file);
$files = preg_grep('/\.'.preg_quote($type,'/').'$/i', glob($dir.'/*'));
 
if (count($files) > 0) {
$cs_file = array_pop($files);
} else {
$cs_file = $dir.'/'.basename(dirname($file)).'.'.$type;
}
return $cs_file;
}
 
function cs_add_automatically($file, $type='sfv') {
$type = strtolower($type);
$cs_file = cs_get_checksumfilename($file, $type);
// echo "Checksum file: $cs_file\n";
if ($type == 'sfv') {
if (file_exists($cs_file)) {
$files = sfv_get_files($cs_file);
if (in_arrayi(basename($file), $files)) return true;
} else {
file_put_contents($cs_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
$files = array();
}
$hash = crc32_file($file);
if ($hash === false) {
fwrite(STDERR, "Cannot calculate hash of file '$file'\n");
return false;
}
$crc32 = strtoupper($hash);
file_put_contents($cs_file, basename($file)." $crc32\r\n", FILE_APPEND);
return true;
}
else if ($type == 'md5') {
if (file_exists($cs_file)) {
$files = md5_get_files($cs_file);
if (in_arrayi(basename($file), $files)) return true;
} else {
file_put_contents($cs_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
$files = array();
}
$hash = md5_file($file);
if ($hash === false) {
fwrite(STDERR, "Cannot calculate hash of file '$file'\n");
return false;
}
$md5 = strtolower($hash);
file_put_contents($cs_file, "$md5 *".basename($file)."\r\n", FILE_APPEND);
return true;
}
else if ($type == 'none') {
return true;
}
else {
fwrite(STDERR, "Unknown checksum type '$type'. Must be SFV, MD5 or None\n");
return false;
}
}
 
function md5_get_files($filename) {
// Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/md5_generate.php
$out = array();
$lines = file($filename);
foreach ($lines as $line) {
$line = str_replace("\xEF\xBB\xBF",'',$line);
$line = trim($line);
if ($line == '') continue;
$line = str_replace('*', ' ', $line);
$line = str_replace("\t", ' ', $line);
list($checksum, $origname) = explode(' ', $line, 2);
$origname = dirname($filename) . '/' . trim($origname);
$checksum = trim($checksum);
$out[] = $origname;
}
 
return $out;
}
 
function sfv_get_files($filename) {
// Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php
$out = array();
$lines = file($filename);
foreach ($lines as $line) {
$line = rtrim($line);
if ($line == '') continue;
if (substr($line,0,1) == ';') continue;
$out[] = substr($line, 0, strrpos($line, ' '));
 
}
return $out;
}
 
function swapEndianness($hex) {
// Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php
return implode('', array_reverse(str_split($hex, 2)));
}
 
function crc32_file($filename, $rawOutput = false) {
// Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php
$hash = hash_file('crc32b', $filename, true);
if ($hash === false) return false;
$out = bin2hex($hash);
if (hash('crc32b', 'TEST') == 'b893eaee') {
// hash_file() in PHP 5.2 has the wrong Endianess!
// https://bugs.php.net/bug.php?id=47467
$out = swapEndianness($out);
}
return $out;
}
 
function in_arrayi($needle, $haystack) {
// Source: https://www.php.net/manual/en/function.in-array.php#89256
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
/trunk/new_ffmpeg
0,0 → 1,18
 
With yt-dlp, you might get the error "ERROR: Postprocessing: Conversion failed!" if
you have a tool old ffmpeg version.
 
If you cannot update to a newer version to ffmpeg using aptitude,
you can download (or compile) a new ffmpeg version.
 
Example for downloading FFmpeg 5.1.1 for Raspberry Pi Buster (32 bit ARM)
 
Visit:
https://johnvansickle.com/ffmpeg/
Download and unpack:
wget https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-armhf-static.tar.xz
tar xvf ffmpeg-release-armhf-static.tar.xz
rm ffmpeg-release-armhf-static.tar.xz
Create a symlink:
ln -s ffmpeg-5.1.1-armhf-static/ffmpeg .
 
/trunk/phpstan.neon.dist
0,0 → 1,18
 
# Attention: Before running PHPstan, run "ln -s ytdwn ytdwn.php"
 
parameters:
level: 5
fileExtensions:
- php
- phps
paths:
- .
excludePaths:
analyseAndScan:
- .phpstan.tmp
tmpDir: .phpstan.tmp
ignoreErrors:
- '#is always (true|false)\.#'
- '#Call to function assert\(\) with false will always evaluate to false\.#'
#- '#with no typehint specified\.#'
/trunk/phpstan.sh
0,0 → 1,12
#!/bin/bash
 
DIR=$( dirname "$0 ")
 
cd "$DIR"
 
if [ ! -f ytdwn.php ]; then
ln -s ytdwn ytdwn.php
fi
phpstan
rm ytdwn.php
 
Property changes:
Added: svn:executable
+*
\ No newline at end of property
/trunk/youtube_functions.inc.phps
0,0 → 1,285
<?php
 
// ViaThinkSoft YouTube Downloader Functions 2.3
// Revision: 2022-12-19
// Author: Daniel Marschall <www.daniel-marschall.de>
// Licensed under the terms of the Apache 2.0 License
 
// Get API key: https://console.developers.google.com/apis/credentials
// Test API here: https://developers.google.com/apis-explorer/?hl=de#p/youtube/v3/youtube.playlistItems.list
 
$yt_apikey = null;
$yt_apikey_callback = null;
 
function yt_set_apikey($apikey) {
global $yt_apikey;
$yt_apikey = $apikey;
}
 
function yt_set_apikey_callback($apikey_callback) {
global $yt_apikey_callback;
$yt_apikey_callback = $apikey_callback;
}
 
function yt_get_apikey() {
global $yt_apikey, $yt_apikey_callback;
 
if (!empty($yt_apikey_callback)) {
$apikey = call_user_func($yt_apikey_callback);
if (!yt_check_apikey_syntax($apikey)) throw new Exception("Invalid API key '$apikey'");
} else if (!empty($yt_apikey)) {
$apikey = $yt_apikey;
if (!yt_check_apikey_syntax($apikey)) throw new Exception("Invalid API key '$apikey'");
} else {
throw new Exception("This function requires a YouTube API key.\n");
}
 
return $apikey;
}
 
function yt_playlist_items($playlist_id, $maxresults=-1) {
$out = array();
 
$next_page_token = '';
 
do {
$cont = @file_get_contents('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.urlencode($playlist_id).'&maxResults=50'.(($next_page_token!='') ? '&pageToken='.urlencode($next_page_token) : '').'&key='.urlencode(yt_get_apikey()));
if (!$cont) return false; // e.g. if Playlist was deleted
 
$obj = json_decode($cont, true);
if (!$obj) return false;
 
if (!isset($obj['items'])) return false;
 
foreach ($obj['items'] as $item) {
if ($item['snippet']['resourceId']['kind'] == 'youtube#video') {
$title = $item['snippet']['title'];
$video_id = $item['snippet']['resourceId']['videoId'];
$out[] = array($video_id, $title);
if (($maxresults != -1) && ($maxresults == count($out))) return $out;
}
}
 
$next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
} while ($next_page_token != '');
 
return $out;
}
 
function yt_get_channel_id($username) {
$cont = @file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&forUsername='.urlencode($username).'&part=id');
if (!$cont) return false;
 
$obj = json_decode($cont, true);
if (!$obj) return false;
 
if (!isset($obj['items'])) return false;
 
foreach ($obj['items'] as $item) {
if ($item['kind'] == 'youtube#channel') {
return $item['id'];
}
}
}
 
function yt_get_channel_id_and_stats($username) {
$cont = @file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&forUsername='.urlencode($username).'&part=id,statistics');
if (!$cont) return false;
 
$obj = json_decode($cont, true);
if (!$obj) return false;
 
if (!isset($obj['items'])) return false;
 
foreach ($obj['items'] as $item) {
if ($item['kind'] == 'youtube#channel') {
return array($item['id'], $item['statistics']);
}
}
}
 
function yt_get_channel_stats($channel_id) {
$cont = @file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&id='.urlencode($channel_id).'&part=statistics');
if (!$cont) return false;
 
$obj = json_decode($cont, true);
if (!$obj) return false;
 
if (!isset($obj['items'])) return false; //totalResults could be 0
 
foreach ($obj['items'] as $item) {
if ($item['kind'] == 'youtube#channel') {
return $item['statistics'];
}
}
}
 
function yt_get_playlist_stats($playlist_id) {
$cont = @file_get_contents('https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&id='.urlencode($playlist_id).'&key='.urlencode(yt_get_apikey()));
if (!$cont) return false;
 
$obj = json_decode($cont, true);
if (!$obj) return false;
 
if (!isset($obj['items'])) return false;
 
foreach ($obj['items'] as $item) {
if ($item['kind'] == 'youtube#playlist') {
if (!isset($item['contentDetails']) || is_null($item['contentDetails'])) return false; // can happen to deleted playlists
return $item['contentDetails'];
}
}
}
 
function yt_channel_items($channel_id, $searchterms='', $maxresults=-1) {
$out = array();
 
$next_page_token = '';
 
do {
$cont = @file_get_contents('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='.urlencode($channel_id).(($searchterms!='') ? '&q='.urlencode($searchterms) : '').'&maxResults=50'.(($next_page_token!='') ? '&pageToken='.urlencode($next_page_token) : '').'&key='.urlencode(yt_get_apikey()));
if (!$cont) return false;
 
$obj = json_decode($cont, true);
if (!$obj) return false;
 
if (!isset($obj['items'])) return false;
 
foreach ($obj['items'] as $item) {
if ($item['id']['kind'] == 'youtube#video') {
$title = $item['snippet']['title'];
$video_id = $item['id']['videoId'];
$out[] = array($video_id, $title);
if (($maxresults != -1) && ($maxresults == count($out))) return $out;
}
}
 
$next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
} while ($next_page_token != '');
 
return $out;
}
 
// Acceptable order values are: date, rating, relevance(default), title, videoCount, viewCount
function yt_search_items($searchterms, $order='', $maxresults=-1) {
$out = array();
 
$next_page_token = '';
 
do {
$cont = @file_get_contents('https://www.googleapis.com/youtube/v3/search?part=snippet&q='.urlencode($searchterms).(($order!='') ? '&order='.urlencode($order) : '').'&maxResults=50'.(($next_page_token!='') ? '&pageToken='.urlencode($next_page_token) : '').'&key='.urlencode(yt_get_apikey()));
if (!$cont) return false;
 
$obj = json_decode($cont, true);
if (!$obj) return false;
 
if (!isset($obj['items'])) return false;
 
foreach ($obj['items'] as $item) {
if ($item['id']['kind'] == 'youtube#video') {
$title = $item['snippet']['title'];
$video_id = $item['id']['videoId'];
$out[] = array($video_id, $title);
if (($maxresults != -1) && ($maxresults == count($out))) return $out;
}
}
 
$next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
} while ($next_page_token != '');
 
return $out;
}
 
function getVideoIDFromURL($url) {
// Extract video ID from the URL
 
$vid = false;
$m = null;
 
# Usual format
if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/watch(.*)(/|&|\\?)v=([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
$vid = $m[4];
}
 
# Short format
if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtu\\.be/([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
$vid = $m[2];
}
 
# YouTube "Shorts"
if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/shorts/([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
$vid = $m[2];
}
 
return $vid;
}
 
function getPlaylistIDFromURL($url) {
$pid = false;
 
# Usual format
$m = null;
if (($pid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(.*)(/|&|\\?)list=(.+)&@ismU", $url.'&', $m))) {
$pid = $m[4];
}
 
return $pid;
}
 
function yt_check_apikey_syntax($apikey) {
return preg_match('@^[a-zA-Z0-9]{39}$@', $apikey);
}
 
function yt_check_video_id($video_id) {
return preg_match('@^[a-zA-Z0-9\-_]{11}$@', $video_id);
}
 
function yt_get_channel_id_from_custom_url($custom_url) {
// TODO: is there any API possibility??? API only accepts username and id ?!
 
// https://www.youtube.com/c/SASASMR
// <link rel="canonical" href="https://www.youtube.com/channel/UCp4LfMtDfoa29kTlLnqQ5Mg">
// https://www.youtube.com/impaulsive
// <link rel="canonical" href="https://www.youtube.com/channel/UCGeBogGDZ9W3dsGx-mWQGJA">
// https://www.youtube.com/@KosmonautMusicSpecials
// <link rel="canonical" href="https://www.youtube.com/channel/UCayYOQ0DEIpcTgO9z_b-K1A">
 
$cont = @file_get_contents($custom_url);
if ($cont === false) {
throw new Exception("Cannot open $custom_url using file_get_contents.");
}
if (!preg_match('@<link rel="canonical" href="https://www.youtube.com/channel/([^"]+)">@ismU', $cont, $m)) {
return false;
}
 
return $m[1];
}
 
function yt_get_channel_id_from_url($channel_url) {
$m = null;
if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/user/(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
// Username (deprecated feature. Not every channel has a username associated with it)
// https://www.youtube.com/user/USERNAME
$username = $m[2];
$channel_id = yt_get_channel_id($username);
return $channel_id;
} else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/channel/(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
// Real channel ID
// https://www.youtube.com/channel/ID
$channel_id = $m[2];
return $channel_id;
} else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(c/){0,1}(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
// Channel custom URL
// https://www.youtube.com/NAME or https://www.youtube.com/c/NAME
return yt_get_channel_id_from_custom_url($channel_url);
} else {
return false;
}
}
 
// Examples:
//yt_set_apikey(trim(file_get_contents(__DIR__ . '/.yt_api_key')));
//print_r(yt_playlist_items('PL9GbGAd-gY1pyxZJIX5MOdYdRbdweVAID'));
//print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA'));
//print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA', 'Knight'));
//print_r(yt_search_items('Wesley Willis', 'date', 10));
/trunk/ytdwn
0,0 → 1,1097
#!/usr/bin/php
<?php
 
// ViaThinkSoft YouTube Downloader Util 2.4.3
// Revision: 2024-01-14
// Author: Daniel Marschall <www.daniel-marschall.de>
// Licensed under the terms of the Apache 2.0 License
//
// For syntax and other documentation, please read the file README.
 
// ------------------------------------------------------------------------------------------------
 
error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);
 
define('AUTO_API_KEY', '~/.yt_api_key');
define('AUTO_COOKIE_FILE', '~/.yt_cookies');
define('DOWNLOAD_SIMULATION_MODE', false);
define('DEFAULT_SEARCH_ORDER', 'relevance');
define('DEFAULT_SEARCH_MAXRESULTS', 10);
 
putenv("LANG=de_DE.UTF-8"); // required if video titles contain non-ASCII symbols
 
require_once __DIR__ . '/youtube_functions.inc.phps';
require_once __DIR__ . '/checksum_functions.inc.phps';
 
// Check if we are running in command line
 
if (PHP_SAPI !== 'cli') {
fwrite(STDERR, "Error: Can only run in CLI mode\n");
exit(2);
}
 
// Global vars
 
$listFilenameStack = array();
 
// Default values
 
$allow_creation_outputdir = false;
$type = 'v:';
$outputDir = '';
$alreadyDownloaded = '';
$checksumMode = 'none';
$failList = '';
$failTreshold = 3;
$rest_args = array();
$verbose = false;
$mp3id_transfer = true;
$apikey = '';
$resultcache = '';
$extra_args =
// '-k ' . // The additional "-k" option in the above makes youtube-dl keep downloaded videos.
'-i ' . // continue upon download errors
'-c '; // resume partially downloaded video files
$default_template = '%(title)s-%(id)s.%(ext)s';
$cookie_file = AUTO_COOKIE_FILE;
$downloader = 'yt-dlp';
 
// Parse arguments
// We do not use getopt() at the moment, because the important functionality "optind" is only available in PHP 7.1, which is not yet distributed with most of the stable Linux distros
 
$init_extra_args = false;
$argv_bak = $_SERVER['argv'];
array_shift($argv_bak);
while (count($argv_bak) > 0) {
$arg = array_shift($argv_bak);
$arg2 = $arg . ' ' . (isset($argv_bak[0]) ? $argv_bak[0] : '');
$m = null;
if (preg_match('@^(/t|\-t|\-\-type)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$type = $m[3];
} else if (preg_match('@^(/o|\-o|\-\-outputDir)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$outputDir = $m[3];
} else if (preg_match('@^(/a|\-a|\-\-alreadyDownloaded)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$alreadyDownloaded = $m[3];
} else if (preg_match('@^(/f|\-f|\-\-failList)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$failList = $m[3];
} else if (preg_match('@^(/F|\-F|\-\-failTreshold)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$failTreshold = $m[3];
} else if (preg_match('@^(/C|\-C|\-\-resultcache)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$resultcache = $m[3];
} else if (preg_match('@^(/H|\-H|\-\-checksumMode)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$checksumMode = $m[3];
if ((strtolower($checksumMode) != 'none')
&& (strtolower($checksumMode) != 'sfv')
&& (strtolower($checksumMode) != 'md5')
&& (strtolower($checksumMode) != 'sfv,md5')
&& (strtolower($checksumMode) != 'md5,sfv')) syntax_error("Checksum mode needs to be either 'None', 'MD5', 'SFV', or 'MD5,SFV'.");
} else if (preg_match('@^(/T|\-T|\-\-default-template)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$default_template = $m[3];
} else if (preg_match('@^(/A|\-A|\-\-api-key)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$apikey = file_exists($m[3]) ? trim(file_get_contents($m[3])) : $m[3];
} else if (preg_match('@^(\-\-cookies)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$cookie_file = file_exists($m[3]) ? trim(file_get_contents($m[3])) : $m[3];
} else if (preg_match('@^(\-\-downloader)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$downloader = $m[3];
} else if (preg_match('@^(/X|\-X|\-\-extra-args)(\s+|=)(.*)$@s', $arg2, $m)) {
array_shift($argv_bak);
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
if ($init_extra_args) {
$extra_args .= ' ' . $m[3]; // user has multiple "-X" arguments. append.
} else {
$extra_args = $m[3]; // overwrite defaults
$init_extra_args = true;
}
} else if (preg_match('@^(/\?|/h|\-\?|\-h|\-\-help)$@s', $arg, $m)) {
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
help();
} else if (preg_match('@^(/V|\-V|\-\-version)$@s', $arg, $m)) {
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
version();
} else if (preg_match('@^(/v|\-v|\-\-verbose)$@s', $arg, $m)) {
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$verbose = true;
} else if (preg_match('@^(/N|\-N|\-\-no-mp3-tagtransfer)$@s', $arg, $m)) {
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$mp3id_transfer = false;
} else if (preg_match('@^(/O|\-O|\-\-create-outputdir)$@s', $arg, $m)) {
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$allow_creation_outputdir = true;
} else if ($arg == '--') {
if (count($rest_args) > 0) syntax_error("Invalid argument: ".$rest_args[0]);
$rest_args = $argv_bak;
break;
} else {
$rest_args[] = $arg;
}
}
unset($arg);
unset($argv_bak);
unset($init_extra_args);
 
// Special arguments required for some downloads
 
if (($downloader == 'yt-dlp') && file_exists(__DIR__.'/ffmpeg')) {
// With yt-dlp, you might get the error "ERROR: Postprocessing: Conversion failed!" if
// you have a tool old ffmpeg version.
$extra_args .= ' --ffmpeg-location '.escapeshellarg(__DIR__.'/ffmpeg');
}
 
if ($downloader == 'yt-dlp') {
// https://github.com/yt-dlp/yt-dlp/issues/7872
// Some formats are not downloadable and yt-dlp rather cancels everything instead of just downloading the correct thing...!
// Make sure that we select a format that is downloadable!
$extra_args .= ' --check-formats';
}
 
define('DOWNLOAD_YT_FORK', $downloader);
 
// Validity checks
 
if ((substr($type,0,2) != 'a:') && (substr($type,0,2) != 'v:')) syntax_error("Type must be either 'v:' or 'a:'. '$type' is not valid.");
 
if (count($rest_args) == 0) syntax_error("Please enter at least one desired video for downloading");
 
if ($failTreshold <= 0) syntax_error("Fail treshold has invalid value. Must be >0.");
 
$cookie_file = expand_tilde($cookie_file);
if (!file_exists($cookie_file)) $cookie_file = '';
 
// Try to download/update youtube-dl/yt-dlp into local directory
download_latest_downloader();
 
if (command_exists(__DIR__.'/'.DOWNLOAD_YT_FORK)) {
echo "Will use '".DOWNLOAD_YT_FORK."' from local directory\n";
define('YTDL_EXE', __DIR__.'/'.DOWNLOAD_YT_FORK);
} else {
// Download failed. Is at least a package installed?
if (command_exists(DOWNLOAD_YT_FORK)) {
echo "Will use '".DOWNLOAD_YT_FORK."' from Linux package\n";
define('YTDL_EXE', DOWNLOAD_YT_FORK);
} else {
fwrite(STDERR, "This script requires the tool/package '".DOWNLOAD_YT_FORK."'. Please install it first.\n");
exit(1);
}
}
 
// Now process the videos
 
yt_set_apikey_callback('_getApikey');
 
foreach ($rest_args as $resource) {
if ($verbose) echo "Handle: $resource\n";
if (strpos($resource, ':') === false) {
fwrite(STDERR, "Invalid resource '$resource' (you are missing the prefix, e.g. vurl: or vid:). Skipping.\n");
} else {
list($resourceType, $resourceValue) = explode(':', $resource, 2);
try {
ytdwn_handle_resource($resourceType, $resourceValue);
} catch(Exception $e) {
fwrite(STDERR, "Error at '$resourceType:$resourceValue': ".$e->getMessage()."\n");
}
}
}
 
// ------------------------------------------------------------------------------------------------
 
function ytdwn_handle_resource($resourceType, $resourceValue) {
if ($resourceType == 'vid') {
$video_id = parse_quoting($resourceValue);
ytdwn_video_id($video_id);
} else if ($resourceType == 'vurl') {
$video_url = parse_quoting($resourceValue);
$video_id = getVideoIDFromURL($video_url);
if (!$video_id) {
fwrite(STDERR, "$video_url is not a valid YouTube video URL. Skipping.\n");
} else {
ytdwn_video_id($video_id);
}
} else if ($resourceType == 'pid') {
$playlist_id = parse_quoting($resourceValue);
ytdwn_playlist_id($playlist_id);
} else if ($resourceType == 'purl') {
$playlist_url = parse_quoting($resourceValue);
$playlist_id = getPlaylistIDFromURL($playlist_url);
if (!$playlist_id) {
fwrite(STDERR, "$playlist_url is not a valid YouTube playlist URL. Skipping\n");
} else {
ytdwn_playlist_id($playlist_id);
}
} else if ($resourceType == 'cid') {
$channel_id = parse_quoting($resourceValue);
 
$m = null;
if (preg_match('@\[search=(.+)\]@ismU', $channel_id, $m)) {
$search = $m[1];
$channel_id = preg_replace('@\[search=(.+)\]@ismU', '', $channel_id);
} else {
$search = ''; // default
}
$search = parse_quoting($search);
 
ytdwn_channel_id($channel_id, $search);
} else if ($resourceType == 'cname') {
$channel_name = parse_quoting($resourceValue);
 
$m = null;
if (preg_match('@\[search=(.+)\]@ismU', $channel_name, $m)) {
$search = $m[1];
$channel_name = preg_replace('@\[search=(.+)\]@ismU', '', $channel_name);
} else {
$search = ''; // default
}
$search = parse_quoting($search);
 
$channel_name = parse_quoting($channel_name);
$channel_id = yt_get_channel_id($channel_name);
if (!$channel_id) {
fwrite(STDERR, "URL $channel_name is a valid YouTube username. Will now try to interprete it as channel ID instead....\n");
}
ytdwn_channel_id($channel_id, $search);
} else if ($resourceType == 'curl') {
$channel_url = parse_quoting($resourceValue);
 
$m = null;
if (preg_match('@\[search=(.+)\]@ismU', $channel_url, $m)) {
$search = $m[1];
$channel_url = preg_replace('@\[search=(.+)\]@ismU', '', $channel_url);
} else {
$search = ''; // default
}
$search = parse_quoting($search);
 
$channel_url = parse_quoting($channel_url);
$channel_id = curl_to_cid($channel_url);
if (!$channel_id) {
fwrite(STDERR, "URL $channel_url is a valid YouTube channel or username URL. Skipping\n");
} else {
ytdwn_channel_id($channel_id, $search);
}
} else if ($resourceType == 'search') {
$searchterm = parse_quoting($resourceValue);
 
$order = '';
$m = null;
if (preg_match('@\[order=(.+)\]@ismU', $searchterm, $m)) {
$order = $m[1];
$searchterm = preg_replace('@\[order=(.+)\]@ismU', '', $searchterm);
} else {
$order = DEFAULT_SEARCH_ORDER; // default
}
$order = parse_quoting($order);
 
$maxresults = '';
if (preg_match('@\[maxresults=(.+)\]@ismU', $searchterm, $m)) {
$maxresults = $m[1];
$searchterm = preg_replace('@\[maxresults=(.+)\]@ismU', '', $searchterm);
} else {
$maxresults = DEFAULT_SEARCH_MAXRESULTS; // default
}
$maxresults = parse_quoting($maxresults);
 
$searchterm = parse_quoting($searchterm);
 
ytdwn_search($searchterm, $order, $maxresults);
} else if ($resourceType == 'list') {
$list_files = glob(parse_quoting($resourceValue)); // in case the user entered a wildcard, e.g. *.list
foreach ($list_files as $list_file) {
if (!file_exists($list_file)) {
fwrite(STDERR, "List file $list_file does not exist. Skipping\n");
} else {
ytdwn_list_file($list_file);
}
}
} else {
fwrite(STDERR, "Resource type '$resourceType' is not valid. Skipping $resourceType:$resourceValue.\n");
}
}
 
function ytdwn_list_file($list_file) {
global $listFilenameStack, $verbose;
 
if ($verbose) echo "Processing list file '$list_file' ...\n";
 
$listFilenameStack[] = $list_file;
$lines = file($list_file);
foreach ($lines as $line) {
$line = trim($line);
if ($line == '') continue;
if ($line[0] == '#') continue;
if (strpos($line, ':') === false) {
fwrite(STDERR, "Invalid resource '$line' (you are missing the prefix, e.g. vurl: or vid:). Skipping.\n");
} else {
list($resourceType, $resourceValue) = explode(':',$line,2);
try {
ytdwn_handle_resource($resourceType, $resourceValue);
} catch(Exception $e) {
fwrite(STDERR, "Error at line '$line': ".$e->getMessage()."\n");
}
}
}
array_pop($listFilenameStack);
}
 
function ytdwn_channel_id($channel_id, $search='') {
global $type;
global $verbose;
 
if ($verbose) echo "Processing channel ID '$channel_id' ...\n";
 
// List the videos of the channel
 
$use_cache = !empty(_getResultcache()) && file_exists(_getResultcache());
$cont = $use_cache ? file_get_contents(_getResultcache()) : '';
$out = json_decode($cont, true);
if ($out == NULL) $out = array();
 
if ($use_cache) {
$stats = yt_get_channel_stats($channel_id);
if (!$stats) {
fwrite(STDERR, "Cannot get stats for channel with ID '$channel_id'\n");
return;
}
$videocount = $stats['videoCount'];
 
$key = (!empty($search)) ? 'cid:'.$channel_id.'/'.$search : 'cid:'.$channel_id;
 
if (!isset($out[$key])) $out[$key] = array();
$videocount_old = isset($out[$key]['count']) ? $out[$key]['count'] : -1;
} else {
$videocount = -1;
$videocount_old = -2;
$key = '';
}
 
if ($videocount_old != $videocount) { // Attention: This might not work if videos are deleted and added (= the count stays the same)
if ($verbose && $use_cache) echo "Video count changed from $videocount_old to $videocount\n";
$out[$key]['count'] = $videocount;
if (!empty($search)) {
$out[$key]['results'] = yt_channel_items($channel_id, $search);
} else {
$out[$key]['results'] = yt_channel_items($channel_id);
}
if (!$out[$key]['results']) {
// TODO: If a channel has deleted all videos, then this message comes.
// However, technically this is not an error.
fwrite(STDERR, "Cannot get result for channel with ID '$channel_id'\n");
return;
}
} else {
if ($verbose) echo "Video count for channel is still $videocount, keep ".count($out[$key]['results'])." results.\n";
}
 
// Save the cache
 
try {
if ($use_cache) file_put_contents(_getResultcache(), json_encode($out));
} catch(Exception $e) {
fwrite(STDERR, "Cannot write result cache\n");
}
 
// Now download
 
if (!$out[$key]['results']) {
// TODO: If a channel has deleted all videos, then this message comes.
// However, technically this is not an error.
fwrite(STDERR, "Cannot get result for channel with ID '$channel_id'\n");
return;
}
foreach ($out[$key]['results'] as list($id, $title)) {
if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n";
ytdwn_video_id($id);
}
}
 
function ytdwn_playlist_id($playlist_id) {
global $type;
global $verbose;
 
if ($verbose) echo "Processing playlist ID '$playlist_id' ...\n";
 
// List the videos of the playlist
 
$use_cache = !empty(_getResultcache()) && file_exists(_getResultcache());
$cont = $use_cache ? file_get_contents(_getResultcache()) : '';
$out = json_decode($cont, true);
if ($out == NULL) $out = array();
 
if ($use_cache) {
$stats = yt_get_playlist_stats($playlist_id);
if (!$stats) {
fwrite(STDERR, "Cannot get stats for playlist with ID '$playlist_id'\n");
return;
}
$videocount = $stats['itemCount'];
 
$key = 'pid:'.$playlist_id;
 
if (!isset($out[$key])) $out[$key] = array();
$videocount_old = isset($out[$key]['count']) ? $out[$key]['count'] : -1;
} else {
$videocount = -1;
$videocount_old = -2;
$key = '';
}
 
if ($videocount_old != $videocount) { // Attention: This might not work if videos are deleted and added (= the count stays the same)
if ($verbose && $use_cache) echo "Video count changed from $videocount_old to $videocount\n";
$out[$key]['count'] = $videocount;
$out[$key]['results'] = yt_playlist_items($playlist_id);
if (!$out[$key]['results']) {
// TODO: If a playlist has deleted all videos, then this message comes.
// However, technically this is not an error.
fwrite(STDERR, "Cannot get result for playlist with ID '$playlist_id'\n");
return;
}
} else {
if ($verbose) echo "Video count for playlist is still $videocount, keep ".count($out[$key]['results'])." results.\n";
}
 
// Save the cache
 
try {
if ($use_cache) file_put_contents(_getResultcache(), json_encode($out));
} catch(Exception $e) {
fwrite(STDERR, "Cannot write result cache\n");
}
 
// Now download
 
if (!$out[$key]['results']) {
fwrite(STDERR, "Cannot get result for playlist with ID '$playlist_id'\n");
return;
}
foreach ($out[$key]['results'] as list($id, $title)) {
if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n";
ytdwn_video_id($id);
}
}
 
function ytdwn_search($search, $order='', $maxresults=-1) {
global $type;
global $verbose;
 
if ($verbose) echo "Searching for '$search' ...\n";
 
// Perform the search and list the videos
 
$results = yt_search_items($search, $order, $maxresults);
 
// Now download
 
if (!$results) {
fwrite(STDERR, "Cannot get data for search '$search'\n");
return;
}
foreach ($results as list($id, $title)) {
if ($verbose) echo "Downloading '$title' as ".hf_type($type)." ...\n";
ytdwn_video_id($id);
}
}
 
function template_to_wildcard($template, $video_id) {
$x = $template;
$x = str_replace('%(id)s', $video_id, $x);
$x = preg_replace('@%\\(.+\\)s@ismU', '*', $x);
$x = preg_replace('@\\*+@', '*', $x);
return $x;
}
 
function ytdwn_get_downloaded_filename($outputTemplate, $video_id) {
if (strpos($outputTemplate, '%(id)s') === false) {
// TODO: There needs to be a better way to find out the written file name !!!
return false;
} else {
$wildcard = template_to_wildcard($outputTemplate, $video_id);
$test = glob($wildcard);
if (count($test) == 0) return false;
return $test[0];
}
}
 
function ytdwn_video_id($video_id) {
global $type;
global $verbose;
global $mp3id_transfer;
global $extra_args;
global $default_template;
global $failTreshold;
global $cookie_file;
global $checksumMode;
 
if (DOWNLOAD_SIMULATION_MODE) {
echo "SIMULATE download of video id $video_id as ".hf_type($type)." to "._getOutputDir()."\n";
return;
}
 
if (!empty(_getAlreadyDownloaded()) && in_alreadydownloaded_file($type, $video_id)) {
if ($verbose) echo "Video $video_id has already been downloaded. Skip.\n";
return true;
}
 
if (!empty(_getFailList()) && (ytdwn_fail_counter($type, $video_id) >= $failTreshold)) {
if ($verbose) echo "Video $video_id has failed too often. Skip.\n";
return true;
}
 
$out = '';
$code = -1;
 
$outputTemplate = rtrim(_getOutputDir(), '/').'/'.$default_template;
 
if (substr($type,0,2) == 'v:') {
$format = substr($type,2);
if (!empty($format)) {
$cmd = YTDL_EXE.' -o '.escapeshellarg($outputTemplate).' '.$extra_args.(empty($cookie_file) ? '' : ' --cookies '.$cookie_file).' '.escapeshellarg(vid_to_vurl($video_id)).' --format '.escapeshellarg($format);
echo "$cmd\n";
exec($cmd, $out, $code);
} else {
$cmd = YTDL_EXE.' -o '.escapeshellarg($outputTemplate).' '.$extra_args.(empty($cookie_file) ? '' : ' --cookies '.$cookie_file).' '.escapeshellarg(vid_to_vurl($video_id));
echo "$cmd\n";
exec($cmd, $out, $code);
}
 
$written_file = $code == 0 ? ytdwn_get_downloaded_filename($outputTemplate, $video_id) : false;
 
} else if (substr($type,0,2) == 'a:') {
$format = substr($type,2);
if (!empty($format)) {
$cmd = YTDL_EXE.' -o '.escapeshellarg($outputTemplate).' '.$extra_args.(empty($cookie_file) ? '' : ' --cookies '.$cookie_file).' '.escapeshellarg(vid_to_vurl($video_id)).' --extract-audio --audio-format '.escapeshellarg($format);
echo "$cmd\n";
exec($cmd, $out, $code);
} else {
$cmd = YTDL_EXE.' -o '.escapeshellarg($outputTemplate).' '.$extra_args.(empty($cookie_file) ? '' : ' --cookies '.$cookie_file).' '.escapeshellarg(vid_to_vurl($video_id)).' --extract-audio';
echo "$cmd\n";
exec($cmd, $out, $code);
}
 
$written_file = $code == 0 ? ytdwn_get_downloaded_filename($outputTemplate, $video_id) : false;
 
if (($code == 0) && ($mp3id_transfer) && (strtolower($format) == 'mp3')) {
if (!$written_file) {
fwrite(STDERR, "Cannot include YouTube ID to MP3, because the default template does not contain '%(id)s', or the downloaded file could not be determined for another reason.\n");
} else {
mp3_transfer_vid_to_id($written_file, $video_id);
}
}
} else {
assert(false);
return false;
}
 
if ($code == 0) {
if ($verbose) {
fwrite(STDOUT, "Successfully downloaded video ID $video_id as ".hf_type($type)."\n");
if ($written_file) fwrite(STDOUT, "Output file name: $written_file\n");
}
if (!empty(_getAlreadyDownloaded())) {
try {
addto_alreadydownloaded_file($type, $video_id);
} catch(Exception $e) {
fwrite(STDERR, "Cannot add to 'already downloaded' file\n");
}
}
 
// Now do the checksums
foreach (explode(',',$checksumMode) as $mode) {
if (strtolower($mode) === 'none') continue;
if (!$written_file) {
fwrite(STDERR, "Cannot add to the '$mode' checksum file, because the default template does not contain '%(id)s', or the downloaded file could not be determined for another reason.\n");
} else if (!cs_add_automatically($written_file, $mode)) {
fwrite(STDERR, "Could not write to '$mode' checksum file!\n");
}
}
} else {
fwrite(STDERR, "Error downloading $video_id! (Code $code)\n");
if (!empty(_getFailList())) {
try {
ytdwn_register_fail($type, $video_id, $code);
} catch(Exception $e) {
fwrite(STDERR, "Cannot register fail\n");
}
}
return false;
}
 
return true;
}
 
function vid_to_vurl($video_id) {
return "https://www.youtube.com/watch?v=$video_id";
}
 
function EndsWith($Haystack, $Needle){
return strrpos($Haystack, $Needle) === strlen($Haystack)-strlen($Needle);
}
 
/**
* Tries to put the video ID into the MP3 meta tags, and then removes the ID
* from the file name.
* @param $written_file
* @param $video_id
* @return bool
*/
function mp3_transfer_vid_to_id(&$written_file, $video_id) {
global $verbose;
global $default_template;
 
if (!command_exists('id3v2')) {
fwrite(STDERR, "Tool id3v2 is not installed. Will not transfer the YouTube ID into the MP3 ID Tag. Use paramter '-N' to stop trying the transfer.\n");
return false;
}
 
$orig_ts = filemtime($written_file);
$ec = -1;
system('id3v2 -c '.escapeshellarg($video_id).' '.escapeshellarg($written_file), $ec);
touch($written_file, $orig_ts);
if ($ec != 0) {
fwrite(STDERR, "Cannot set ID tag for file $written_file\n");
return false;
}
 
$target_filename = $written_file;
 
// Things like '<title>-<id>.mp3' become '<title>.mp3' (our default template)
// But templates like '<title> (<id>).mp3' could become '<title> ().mp3', which is not nice
// So, we try our best to find the most common template types...
$target_filename = str_replace('-'.$video_id, '', $target_filename);
$target_filename = str_replace('_'.$video_id, '', $target_filename);
$target_filename = str_replace(' '.$video_id, '', $target_filename);
$target_filename = str_replace('('.$video_id.')', '', $target_filename);
$target_filename = str_replace('['.$video_id.']', '', $target_filename);
$target_filename = str_replace($video_id, '', $target_filename); // must be the last!
if ($target_filename === $written_file) {
fwrite(STDERR, "Could not remove VideoID from filename '$written_file'\n"); // should not happen
return false;
}
 
if (!intelligent_rename($written_file, $target_filename)) {
fwrite(STDERR, "Could not rename '$written_file' to '$target_filename'\n");
return false;
}
 
$written_file = $target_filename; // was modified by intelligent_rename()
return true;
}
 
function curl_to_cid($channel_url) {
return yt_get_channel_id_from_url($channel_url);
}
 
function in_alreadydownloaded_file($type, $video_id) {
$lines = file(_getAlreadyDownloaded());
foreach ($lines as $line) {
if (trim($line) == rtrim($type,':').':'.$video_id) {
return true;
}
}
return false;
}
 
function addto_alreadydownloaded_file($type, $video_id) {
file_put_contents(_getAlreadyDownloaded(), rtrim($type,':').':'.$video_id."\n", FILE_APPEND);
}
 
function syntax_error($msg) {
fwrite(STDERR, "Syntax error: ".trim($msg)."\n");
fwrite(STDERR, "Please use argument '--help' to show the syntax rules.\n");
exit(2);
}
 
function _help() {
global $argv;
$out = '';
$own = file_get_contents($argv[0]);
$help = explode('// ----', $own, 2)[0];
$m = null;
$help = preg_match_all('@^//(.*)$@mU', $help, $m);
foreach ($m[1] as $line) {
$out .= substr($line,1)."\n";
}
return $out;
}
 
function help() {
echo _help();
exit(0);
}
 
function version() {
echo explode("\n\n", _help(), 2)[0]."\n";
exit(0);
}
 
function command_exists($command) {
// https://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
 
$ec = -1;
system('command -v '.escapeshellarg($command).' > /dev/null', $ec);
return ($ec == 0);
}
 
function hf_type($type) {
if (strpos($type, ':') === false) return $type; // invalid type (missing ':')
list($av, $format) = explode(':', $type);
 
if ($av == 'a') $av = 'audio';
else if ($av == 'v') $av = 'video';
else return $type; // invalid type
 
return (!empty($format)) ? $format.'-'.$av : $av;
}
 
function expand_tilde($path) {
// Source: http://jonathonhill.net/2013-09-03/tilde-expansion-in-php/
 
if (function_exists('posix_getuid') && strpos($path, '~') !== false) {
$info = posix_getpwuid(posix_getuid());
$path = str_replace('~', $info['dir'], $path);
}
 
return $path;
}
 
function _getLastListname() {
global $listFilenameStack;
$listname = ''; // default
if (count($listFilenameStack) > 0) {
$listname = $listFilenameStack[count($listFilenameStack)-1];
$listname = pathinfo($listname, PATHINFO_FILENAME); // remove file extension, e.g. ".list"
}
return $listname;
}
 
function _getApiKey() {
global $apikey;
 
$out = $apikey;
if (empty($out)) {
$auto_api_key = AUTO_API_KEY;
$auto_api_key = expand_tilde($auto_api_key);
$auto_api_key = str_replace('[listname]', _getLastListname(), $auto_api_key);
 
if (file_exists($auto_api_key)) {
$out = trim(file_get_contents($auto_api_key));
} else {
syntax_error("YouTube API key not found in file '$auto_api_key'. To specify the path, use the argument '-A'");
}
} else {
$out = str_replace('[listname]', _getLastListname(), $out);
$out = expand_tilde($out);
 
if (file_exists($out)) {
$out = trim(file_get_contents($out));
} else {
// Assume, $out is a key, not a file
}
}
 
if (!yt_check_apikey_syntax($out)) syntax_error("'$out' is not a valid API key, not an existing file containing an API key.");
 
return $out;
}
 
function _getResultCache() {
global $resultcache;
if (empty($resultcache)) return '';
 
$out = expand_tilde($resultcache);
 
$out = str_replace('[listname]', _getLastListname(), $out);
 
if (!file_exists($out)) {
@touch($out);
if (!file_exists($out)) {
fwrite(STDERR, "File '$out' cannot be created. Disable feature.\n");
return '';
}
}
 
return $out;
}
 
function _getAlreadyDownloaded() {
global $alreadyDownloaded;
if (empty($alreadyDownloaded)) return '';
 
$out = expand_tilde($alreadyDownloaded);
 
$out = str_replace('[listname]', _getLastListname(), $out);
 
if (!file_exists($out)) {
@touch($out);
if (!file_exists($out)) {
fwrite(STDERR, "File '$out' cannot be created. Disable feature.\n");
return '';
}
}
 
return $out;
}
 
function _getFailList() {
global $failList;
if (empty($failList)) return '';
 
$out = expand_tilde($failList);
 
$out = str_replace('[listname]', _getLastListname(), $out);
 
if (!file_exists($out)) {
@touch($out);
if (!file_exists($out)) {
fwrite(STDERR, "File '$out' cannot be created. Disable feature.\n");
return '';
}
}
 
return $out;
}
 
function _getOutputDir() {
global $outputDir, $allow_creation_outputdir;
if (empty($outputDir)) return '.';
 
$out = expand_tilde($outputDir);
 
$out = str_replace('[listname]', _getLastListname(), $out);
 
if ($allow_creation_outputdir) {
if (!is_dir($out)) {
mkdir($out, 0777, true);
if (!is_dir($out)) {
fwrite(STDERR, "Output directory '$out' does not exist.\n");
exit(1);
}
}
} else {
if (!is_dir($out)) {
fwrite(STDERR, "Output directory '$out' does not exist.\n");
exit(1);
}
}
 
return $out;
}
 
function parse_quoting($str) {
if ((substr($str,0,1) == '"') && (substr($str,-1,1) == '"')) {
$str = substr($str,1,strlen($str)-2);
 
$escape = false;
$out = '';
for ($i=0; $i<strlen($str); $i++) {
$char = $str[$i];
 
if ($char == '\\') {
if ($escape) {
$out .= $char;
$escape = false;
} else {
$escape = true;
}
} else {
$out .= $char;
}
 
}
$str = $out;
 
}
 
return $str;
}
 
function ytdwn_register_fail($type, $video_id, $code) {
// Note: Error code $code ist currently not used
 
$file = _getFailList();
$cont = file_get_contents($file);
$m = null;
if (preg_match("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU", $cont, $m)) {
$cont = preg_replace("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU",
"Video ID $video_id failed ".(((int)$m[1])+1)." time(s) with type $type", $cont);
file_put_contents($file, $cont);
} else {
file_put_contents($file, "Video ID $video_id failed 1 time(s) with type $type\n", FILE_APPEND);
}
}
 
function ytdwn_fail_counter($type, $video_id) {
$file = _getFailList();
$cont = file_get_contents($file);
$m = null;
if (preg_match("@Video ID ".preg_quote($video_id,'@')." failed (\d+) time\(s\) with type ".preg_quote($type,'@')."@ismU", $cont, $m)) {
return $m[1];
} else {
return 0;
}
}
 
/**
* @param string $fileName
* @param bool $caseSensitive
* @return bool
* @see https://stackoverflow.com/questions/3964793/php-case-insensitive-version-of-file-exists
*/
function file_exists_ext(string $fileName, bool $caseSensitive = true): bool {
if(file_exists($fileName)) {
return true; // $fileName;
}
if($caseSensitive) return false;
 
// Handle case insensitive requests
$directoryName = dirname($fileName);
$fileArray = glob($directoryName . '/*', GLOB_NOSORT);
$fileNameLowerCase = strtolower($fileName);
foreach($fileArray as $file) {
if(strtolower($file) == $fileNameLowerCase) {
return true; // $file;
}
}
return false;
}
 
function intelligent_rename($src, &$dest) {
$pos = strrpos($dest, '.');
$ext = substr($dest, $pos);
$namewoext = substr($dest, 0, $pos);
$duplicate_count = 1;
$dest_neu = $dest;
while (file_exists_ext($dest_neu, false)) {
$duplicate_count++;
$dest_neu = "$namewoext ($duplicate_count)$ext";
}
$res = rename($src, $dest_neu);
if ($res) $dest = $dest_neu;
return $res;
}
 
function download_latest_downloader() {
$download_url = false;
 
if (DOWNLOAD_YT_FORK == 'yt-dlp') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/SHA2-256SUMS');
#curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cont = curl_exec($ch);
$m = null;
if (preg_match('@^(.+)\s+yt-dlp$@ismU', $cont, $m)) {
$newest_version_sha2 = $m[1];
} else {
$newest_version_sha2 = false;
}
 
if (!$newest_version_sha2) {
fwrite(STDERR, "Failed to get SHA2 sum of latest version of '".DOWNLOAD_YT_FORK."' online. Will not try to download/update '".DOWNLOAD_YT_FORK."' into local directory.\n");
} else {
if (!file_exists(__DIR__.'/'.DOWNLOAD_YT_FORK) || ($newest_version_sha2 != hash_file('sha256',__DIR__.'/'.DOWNLOAD_YT_FORK))) {
$download_url = 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp';
}
}
}
 
if (DOWNLOAD_YT_FORK == 'youtube-dlc') {
// TODO: What is the difference between these two? Which one should be chosen?!
// https://github.com/blackjack4494/yt-dlc (18372 commits, last commit 23 Aug 2021, last release 2020.11.11-3)
// https://github.com/blackjack4494/youtube-dlc (18888 commits, last commit 26 Jul 2021, last release 2020.10.09)
 
// TODO: yt-dlc The sha256 does not fit to the binary! Therefore, it is always downloaded!
// TODO: youtube-dlc has no hashes online...
/*
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://github.com/blackjack4494/yt-dlc/releases/latest/download/SHA2-256SUMS');
#curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cont = curl_exec($ch);
$m = null;
if (preg_match('@^youtube\-dlc:(.+)$@ismU', $cont, $m)) {
$newest_version_sha2 = $m[1];
} else {
$newest_version_sha2 = false;
}
 
if (!$newest_version_sha2) {
fwrite(STDERR, "Failed to get SHA2 sum of latest version of '".DOWNLOAD_YT_FORK."' online. Will not try to download/update '".DOWNLOAD_YT_FORK."' into local directory.\n");
} else {
if (!file_exists(__DIR__.'/'.DOWNLOAD_YT_FORK) || ($newest_version_sha2 != hash_file('sha256',__DIR__.'/'.DOWNLOAD_YT_FORK))) {
$download_url = 'https://github.com/blackjack4494/yt-dlc/releases/latest/download/youtube-dlc';
}
}
*/
 
if (!file_exists(__DIR__.'/'.DOWNLOAD_YT_FORK) || (time()-filemtime(__DIR__.'/'.DOWNLOAD_YT_FORK) > 24*60*60)) {
$download_url = 'https://github.com/blackjack4494/yt-dlc/releases/latest/download/youtube-dlc';
}
}
 
if (DOWNLOAD_YT_FORK == 'youtube-dl') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://yt-dl.org/downloads/latest/MD5SUMS');
#curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cont = curl_exec($ch);
$m = null;
if (preg_match('@^(.+) youtube\-dl$@ismU', $cont, $m)) {
$newest_version_md5 = $m[1];
} else {
$newest_version_md5 = false;
}
 
if (!$newest_version_md5) {
fwrite(STDERR, "Failed to get MD5 sum of latest version of '".DOWNLOAD_YT_FORK."' online. Will not try to download/update '".DOWNLOAD_YT_FORK."' into local directory.\n");
} else {
if (!file_exists(__DIR__.'/'.DOWNLOAD_YT_FORK) || ($newest_version_md5 != md5_file(__DIR__.'/'.DOWNLOAD_YT_FORK))) {
$download_url = 'https://yt-dl.org/latest/youtube-dl';
}
}
}
 
if ($download_url) {
// Try to download/update the file in our directory. It should be the newest available, since YT often breaks downloader
if (file_exists(__DIR__.'/'.DOWNLOAD_YT_FORK)) {
echo "Trying to update '".DOWNLOAD_YT_FORK."' in local directory...\n";
} else {
echo "Trying to download '".DOWNLOAD_YT_FORK."' into local directory...\n";
}
 
@chmod(__DIR__.'/'.DOWNLOAD_YT_FORK, 0777); // otherwise we might not be able to write to it
 
if (!($binary = file_get_contents($download_url))) {
fwrite(STDERR, "Failed to download '".DOWNLOAD_YT_FORK."' into local directory (file_get_contents).\n");
} else if (!@file_put_contents(__DIR__.'/'.DOWNLOAD_YT_FORK, $binary)) {
fwrite(STDERR, "Failed to download '".DOWNLOAD_YT_FORK."' into local directory (file_put_contents).\n");
} else {
if (!@chmod(__DIR__.'/'.DOWNLOAD_YT_FORK, 0544)) {
fwrite(STDERR, "Failed to download '".DOWNLOAD_YT_FORK."' into local directory (chmod 544).\n");
@unlink(__DIR__.'/'.DOWNLOAD_YT_FORK); // try to delete, otherwise we might try to execute a non-executable file
}
}
}
}
Property changes:
Added: svn:executable
+*
\ No newline at end of property
/trunk
Property changes:
Added: svn:ignore
+marschall
+test
+ffmpeg*
+youtube-dl
+youtube-dlc
+yt-dlp
+upload_to_vts
+ytdwn.php
+phpstan.neon
+.phpstan.tmp
+phpstan.bat
+phpstan.phar
+phpstan.sh
+