pandora_fcra/utils.py

131 lines
4.8 KiB
Python
Raw Normal View History

2023-11-18 23:05:30 +00:00
import json
2023-11-14 14:06:56 +00:00
import os
import ox
2023-11-14 14:06:56 +00:00
import shutil
2023-11-18 23:05:30 +00:00
import subprocess
2023-11-14 14:06:56 +00:00
import tempfile
2023-11-14 14:06:56 +00:00
from archive.models import File, Stream, User
2023-11-15 17:10:19 +00:00
from archive.external import get_info, add_subtitles
2023-11-14 14:06:56 +00:00
def import_items(items):
import item.models
for data in items:
i = item.models.Item()
i.user = User.objects.all()[0]
i.save()
2023-11-15 17:10:19 +00:00
i.edit(data, True)
i.level = 0
i.save()
2023-11-14 14:06:56 +00:00
load_vimeo(i)
2023-11-15 17:10:19 +00:00
print(i)
2023-11-14 14:06:56 +00:00
2023-11-18 23:05:30 +00:00
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)
orig = None
for url in urls:
2023-11-14 14:06:56 +00:00
info = get_info(url, None)
media = info[0]
2023-11-18 10:51:20 +00:00
#for resolution in (720, 360):
for resolution in (720,):
tmp = tempfile.mkdtemp()
if isinstance(tmp, bytes):
tmp = tmp.decode('utf-8')
os.chdir(tmp)
2023-11-18 23:05:30 +00:00
cmd = [
'yt-dlp', '-q', url,
'-o', '%(title)80s.%(ext)s'
]
cmd += [
2023-11-14 14:06:56 +00:00
'-f', 'bestvideo[height<=%s][ext=mp4]+bestaudio[ext=m4a]' % resolution,
'--merge-output-format', 'mp4'
]
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
2023-11-18 23:05:30 +00:00
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
for name in parts:
name = os.path.join(tmp, name)
oshash = ox.oshash(name)
if orig is None:
f, created = File.objects.get_or_create(oshash=oshash)
if created:
orig = f
f.item = item
2023-11-14 14:06:56 +00:00
f.info = ox.avinfo(name)
f.info['extension'] = media['extension']
f.info['url'] = url
f.path = '%(title)s.%(extension)s' % media
f.parse_info()
f.selected = True
2023-11-14 14:06:56 +00:00
f.queued = False
if len(parts) > 1:
f.part = part
part += 1
f.save()
f.item.save()
else:
2023-11-18 23:05:30 +00:00
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))
shutil.move(name, stream.media.path)
stream.available = True
if resolution == 720:
source = stream
else:
stream.source = source
stream.save()
stream.make_timeline()
if resolution == 720 and len(parts) == 1:
add_subtitles(item, media, tmp)
2023-11-15 17:10:19 +00:00
os.chdir(cdir)
shutil.rmtree(tmp)
2023-11-18 10:50:33 +00:00
item.update_timeline()
item.save()