This commit is contained in:
Cyberes 2019-12-16 23:27:08 -07:00
parent 779a68e40d
commit 653aded23e
2 changed files with 42 additions and 35 deletions

View File

@ -38,5 +38,5 @@ Download, compress, and send your YouTube videos.
- `--encrypt`, `-e` Encrypt the compressed 7z archive? If set will ask for password - `--encrypt`, `-e` Encrypt the compressed 7z archive? If set will ask for password
- `--password`, `-p` Provide the password to encrypt the compressed 7z archive with - `--password`, `-p` Provide the password to encrypt the compressed 7z archive with
- `--no-update`, `-nu` Don't update any Pip packages. You'd want to update because youtube-dl updates so much - `--no-update`, `-nu` Don't update any Pip packages. You'd want to update because youtube-dl updates so much
- `--max-size`, `-m` Max size of video in mb. Default is 2000 (2 gb) - `--max-size`, `-m` Max size of video in mb. Default is 2000 mb (2 gb)
- `--check-size`, `-c` Verify the video is smaller than the max size and skip if not - `--check-size`, `-c` Verify the video is smaller than the max size and skip if not

View File

@ -30,7 +30,7 @@ parser.add_argument('--encrypt', '-e', action='store_true', help='encrypt the co
parser.add_argument('--password', '-p', action='store_true', help='password to encrypt the compressed 7z archive with') parser.add_argument('--password', '-p', action='store_true', help='password to encrypt the compressed 7z archive with')
parser.add_argument('--no-update', '-nu', action='store_true', help='don\t update Pip packages') parser.add_argument('--no-update', '-nu', action='store_true', help='don\t update Pip packages')
parser.add_argument('--cmd', '-c', help='use the bash youtube-dl instead of the embedded python version', action='store_true') parser.add_argument('--cmd', '-c', help='use the bash youtube-dl instead of the embedded python version', action='store_true')
parser.add_argument('--max-size', '-m', type=int, choices=range(500, 100000), default=2000, help="max size of video in mb") parser.add_argument('--max-size', '-m', type=int, default=2000, help="max size of video in mb")
parser.add_argument('--check-size', '-ch', action='store_true', help="verify the video is smaller than the max size and skip if not") parser.add_argument('--check-size', '-ch', action='store_true', help="verify the video is smaller than the max size and skip if not")
args = parser.parse_args() args = parser.parse_args()
@ -102,7 +102,9 @@ def compress(name):
if args.output is not None: if args.output is not None:
if args.encrypt: if args.encrypt:
pwd = input('password: ') pwd = input('password: ')
cmd = '7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=1024m -ms=on -mhe=on -p"{}.7z" "{}" "downloads/*"'.format(pwd, name) cmd = '7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=1024m -ms=on -mhe=on -p"{}" "{}.7z" "downloads/*"'.format(pwd, name)
elif args.password is not None:
cmd = '7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=1024m -ms=on -mhe=on -p{} "{}.7z" "downloads/*"'.format(args.password, name)
else: else:
cmd = '7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=1024m -ms=on -mhe=on "{}.7z" "downloads/*"'.format(name) cmd = '7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=1024m -ms=on -mhe=on "{}.7z" "downloads/*"'.format(name)
os.system(cmd) os.system(cmd)
@ -115,13 +117,14 @@ def checkSize(url):
ytdl = youtube_dl.YoutubeDL() ytdl = youtube_dl.YoutubeDL()
info = ytdl.extract_info(url, download=False) info = ytdl.extract_info(url, download=False)
max = 0 max = 0
maxBytes = args.max_size * 1000000
for x in info['formats']: for x in info['formats']:
try: try:
if x['filesize'] > max: if x['filesize'] > max:
max = x['filesize'] max = x['filesize']
except TypeError as e: except TypeError as e:
pass pass
if max > args.max_size: if max > maxBytes:
return False return False
else: else:
return True return True
@ -131,6 +134,10 @@ def checkSize(url):
def getUrls(playlist): def getUrls(playlist):
proc = subprocess.run(['bash', 'get-urls.sh', playlist], capture_output=True, encoding="utf-8") proc = subprocess.run(['bash', 'get-urls.sh', playlist], capture_output=True, encoding="utf-8")
if proc.stdout == '':
logger.error('missing get-urls.sh')
print('missing get-urls.sh')
sys.exit(1)
if proc.stdout.find('get-urls.sh: line 1: jq: command not found') == -1: if proc.stdout.find('get-urls.sh: line 1: jq: command not found') == -1:
ret = re.findall(r'(https:\/\/.*)', proc.stdout) ret = re.findall(r'(https:\/\/.*)', proc.stdout)
return ret return ret
@ -183,7 +190,7 @@ def my_hook(d):
sys.stdout.flush() sys.stdout.flush()
ytdlFormat = '(bestvideo[{}][vcodec^=av01][height>=1080][fps>30]/bestvideo[{}][vcodec=vp9.2][height>=1080][fps>30]/bestvideo[{}][vcodec=vp9][height>=1080][fps>30]/bestvideo[{}][vcodec^=av01][height>=1080]/bestvideo[{}][vcodec=vp9.2][height>=1080]/bestvideo[{}][vcodec=vp9][height>=1080]/bestvideo[{}][height>=1080]/bestvideo[{}][vcodec^=av01][height>=720][fps>30]/bestvideo[{}][vcodec=vp9.2][height>=720][fps>30]/bestvideo[{}][vcodec=vp9][height>=720][fps>30]/bestvideo[{}][vcodec^=av01][height>=720]/bestvideo[{}][vcodec=vp9.2][height>=720]/bestvideo[{}][vcodec=vp9][height>=720]/bestvideo[{}][height>=720]/bestvideo[{}])+(bestaudio[acodec=opus]/bestaudio)/best'.replace('{}', 'filesize<' + str(args.max_size)) ytdlFormat = '(bestvideo[{}][vcodec^=av01][height>=1080][fps>30]/bestvideo[{}][vcodec=vp9.2][height>=1080][fps>30]/bestvideo[{}][vcodec=vp9][height>=1080][fps>30]/bestvideo[{}][vcodec^=av01][height>=1080]/bestvideo[{}][vcodec=vp9.2][height>=1080]/bestvideo[{}][vcodec=vp9][height>=1080]/bestvideo[{}][height>=1080]/bestvideo[{}][vcodec^=av01][height>=720][fps>30]/bestvideo[{}][vcodec=vp9.2][height>=720][fps>30]/bestvideo[{}][vcodec=vp9][height>=720][fps>30]/bestvideo[{}][vcodec^=av01][height>=720]/bestvideo[{}][vcodec=vp9.2][height>=720]/bestvideo[{}][vcodec=vp9][height>=720]/bestvideo[{}][height>=720]/bestvideo[{}])+(bestaudio[acodec=opus]/bestaudio)/best'.replace('{}', 'filesize<' + str(args.max_size) + 'M')
ydl_opts = { ydl_opts = {
'merge_output_format': 'mkv', 'merge_output_format': 'mkv',
'allsubtitles': True, 'allsubtitles': True,
@ -198,7 +205,6 @@ ydl_opts = {
'logger': ytdlLogger(), 'logger': ytdlLogger(),
'progress_hooks': [my_hook], 'progress_hooks': [my_hook],
} }
ytdlCMD = 'youtube-dl -i --add-metadata --all-subs --embed-subs --embed-thumbnail -f "{}" --merge-output-format mkv -o "downloads/%(title)s - %(id)s.%(ext)s" --write-annotations --write-info-json --write-description --write-all-thumbnails --write-sub --sub-format "best" --geo-bypass'.format( ytdlCMD = 'youtube-dl -i --add-metadata --all-subs --embed-subs --embed-thumbnail -f "{}" --merge-output-format mkv -o "downloads/%(title)s - %(id)s.%(ext)s" --write-annotations --write-info-json --write-description --write-all-thumbnails --write-sub --sub-format "best" --geo-bypass'.format(
ytdlFormat) ytdlFormat)
@ -208,10 +214,8 @@ if not isURL:
for x in getUrls(line): for x in getUrls(line):
if checkSize(x): if checkSize(x):
if args.check_size: if args.check_size:
if args.cmd:
print('========================') print('========================')
print('[info] Video size ok') print('[info] Video size ok')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
if args.cmd: if args.cmd:
ytOut = subprocess.run(ytdlCMD + ' "' + x + '"', capture_output=True, encoding="utf-8", shell=True) ytOut = subprocess.run(ytdlCMD + ' "' + x + '"', capture_output=True, encoding="utf-8", shell=True)
ret = re.findall(r'(.*\n)', ytOut.stdout) ret = re.findall(r'(.*\n)', ytOut.stdout)
@ -219,21 +223,21 @@ if not isURL:
logger.info(x.strip('\n')) logger.info(x.strip('\n'))
print(x.strip('\n')) print(x.strip('\n'))
else: else:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([x]) ydl.download([x])
if args.playlists: if args.playlists:
compress('{}-{}'.format(args.output, i)) compress('{}-{}'.format(args.output, i))
else: else:
compress(args.output) compress(args.output)
i = i + 1
else: else:
print('[info] Video too big') print('[info] Video too big')
logger.info('skipping ' + x + ' because too big') logger.info('skipping ' + x + ' because too big')
i = i + 1 i = i + 1
else: else:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
for x in getUrls(args.file): for x in getUrls(args.file):
if checkSize(x): if checkSize(x):
if args.check_size: if args.check_size:
if args.cmd:
print('========================') print('========================')
print('[info] Video size ok') print('[info] Video size ok')
if args.cmd: if args.cmd:
@ -243,10 +247,13 @@ else:
logger.info(x.strip('\n')) logger.info(x.strip('\n'))
print(x.strip('\n')) print(x.strip('\n'))
else: else:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([x]) ydl.download([x])
i = i + 1
else: else:
print('[info] Video too big') print('[info] Video too big')
logger.info('skipping ' + x + ' because too big') logger.info('skipping ' + x + ' because too big')
i = i + 1
compress(args.output) compress(args.output)
print() print()