import video only content too

This commit is contained in:
j 2023-11-19 00:05:30 +01:00
parent 678093e456
commit 8facb9e5e7
1 changed files with 47 additions and 4 deletions

View File

@ -1,7 +1,8 @@
import json
import os
import subprocess
import ox
import shutil
import subprocess
import tempfile
from archive.models import File, Stream, User
@ -20,6 +21,17 @@ def import_items(items):
load_vimeo(i)
print(i)
def load_formats(url):
cmd = ['yt-dlp', '-q', url, '-j', '-F']
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
formats = stdout.decode().strip().split('\n')[-1]
return json.loads(formats)
def load_vimeo(item):
urls = [url for url in item.data.get("links", []) if "vimeo.com" in url]
cdir = os.path.abspath(os.curdir)
@ -33,8 +45,10 @@ def load_vimeo(item):
if isinstance(tmp, bytes):
tmp = tmp.decode('utf-8')
os.chdir(tmp)
cmd = ['yt-dlp', '-q', url]
cmd += ['-o', '%(title)80s.%(ext)s']
cmd = [
'yt-dlp', '-q', url,
'-o', '%(title)80s.%(ext)s'
]
cmd += [
'-f', 'bestvideo[height<=%s][ext=mp4]+bestaudio[ext=m4a]' % resolution,
'--merge-output-format', 'mp4'
@ -43,6 +57,35 @@ def load_vimeo(item):
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
if stderr and b'Requested format is not available.' in stderr:
formats = load_formats(url)
has_audio = bool([fmt for fmt in formats['formats'] if fmt['resolution'] == 'audio only'])
has_video = bool([fmt for fmt in formats['formats'] if 'x' in fmt['resolution']])
cmd = [
'yt-dlp', '-q', url,
'-o', '%(title)80s.%(ext)s'
]
if has_video and not has_audio:
cmd += [
'-f', 'bestvideo[height<=%s][ext=mp4]' % resolution,
]
elif not has_video and has_audio:
cmd += [
'bestaudio[ext=m4a]'
]
else:
cmd = []
if cmd:
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
if stdout or stderr:
print(" ".join(cmd))
print(stdout)
print(stderr)
parts = list(os.listdir(tmp))
if parts:
part = 1
@ -67,7 +110,7 @@ def load_vimeo(item):
f.save()
f.item.save()
else:
return 'file exists'
return 'file exists %s' % oshash
stream, created = Stream.objects.get_or_create(file=orig, resolution=resolution, format="mp4")
stream.media.name = stream.path(stream.name())
ox.makedirs(os.path.dirname(stream.media.path))