This repository has been archived on 2026-11-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

95 lines
3.3 KiB
Python

import json
import subprocess
from pathlib import Path
from . import config
def _probe(path):
cmd = [config.FFPROBE, "-v", "error", "-select_streams", "v:0",
"-show_entries", "stream=width,height,duration",
"-of", "json", str(path)]
out = subprocess.run(cmd, capture_output=True, text=True)
return json.loads(out.stdout or "{}")
def _run(cmd):
return subprocess.run(cmd, capture_output=True, text=True)
def transcode(url, vid_id, src_path):
out_dir = config.OPTIMIZED
base = out_dir / f"{vid_id}"
out_file = Path(f"{base}.mp4")
src_path = Path(src_path)
info = _probe(src_path)
streams = info.get("streams") or [{}]
s = streams[0] if streams else {}
width, height = s.get("width"), s.get("height")
duration = s.get("duration")
if duration:
try:
duration = float(duration)
except (TypeError, ValueError):
duration = None
vf = []
if width and height:
if height > 1080:
vf.append("scale=-2:1080")
vf.append("format=yuv420p")
cmd = [config.FFMPEG, "-y", "-i", str(src_path),
"-map", "0:v:0", "-map", "0:a:0?",
"-c:v", "libx264", "-preset", "veryfast", "-crf", config.VIDEO_CRF,
"-profile:v", "high", "-level", "4.1",
"-c:a", "aac", "-b:a", config.AUDIO_BITRATE,
"-movflags", "+faststart", "-threads", "0"]
if vf:
cmd += ["-vf", ",".join(vf)]
cmd.append(str(out_file := base.with_suffix(".mp4")))
_run(cmd)
poster = out_dir / f"{vid_id}_poster.jpg"
thumb = out_dir / f"{vid_id}_thumb.jpg"
preview = out_dir / f"{vid_id}_preview.mp4"
dur = duration or 0
poster_at = max(1.0, dur * 0.08)
preview_at = max(1.0, dur * 0.02)
preview_len = min(10, max(6, int(dur * 0.015))) if dur else 6
# Skapa poster/thumb (extrahera en frame)
_run([config.FFMPEG, "-y", "-ss", str(poster_at), "-i", str(src_path),
"-frames:v", "1", "-vf", "scale=1280:-2,format=yuv420p",
str(poster)])
_run([config.FFMPEG, "-y", "-ss", str(poster_at), "-i", str(src_path),
"-frames:v", "1", "-vf", "scale=480:-2,format=yuv420p",
str(thumb)])
# Preview: short clip, muted webm-liknande mp4 (utan faststart-i-huvud)
_run([config.FFMPEG, "-y", "-ss", str(preview_at), "-i", str(src_path),
"-t", str(preview_len),
"-vf", "scale=848:-2,fps=24,format=yuv420p",
"-c:v", "libx264", "-preset", "veryfast", "-crf", "28",
"-c:a", "aac", "-b:a", "96K",
str(preview)])
# Verifiera output och rapportera verkliga dims
out_info = _probe(out_file)
out_s = (out_info.get("streams") or [{}])[0]
out_w, out_h = out_s.get("width"), out_s.get("height")
size = src_path.stat().st_size if src_path.exists() else None
return {
"width": out_w or width,
"height": out_h or height,
"duration": round(duration, 1) if duration else None,
"quality": f"{out_h or height}p" if (out_h or height) else None,
"size_bytes": size,
"optimized_path": str(out_file) if out_file.exists() else None,
"poster_path": str(poster) if poster.exists() else None,
"thumb_path": str(thumb) if thumb.exists() else None,
"preview_path": str(preview) if preview.exists() and preview.stat().st_size > 0 else None,
}