Add wav transcoding option

This commit is contained in:
Toby Kohlhagen 2025-08-03 23:26:13 +10:00 committed by ed
parent a38e6e65d5
commit b469db3c62
5 changed files with 58 additions and 10 deletions

View file

@ -4684,7 +4684,7 @@ class HttpCli(object):
# for f in fgen: print(repr({k: f[k] for k in ["vp", "ap"]})) # for f in fgen: print(repr({k: f[k] for k in ["vp", "ap"]}))
cfmt = "" cfmt = ""
if self.thumbcli and not self.args.no_bacode: if self.thumbcli and not self.args.no_bacode:
for zs in ("opus", "mp3", "w", "j", "p"): for zs in ("opus", "mp3", "wav", "w", "j", "p"):
if zs in self.ouparam or uarg == zs: if zs in self.ouparam or uarg == zs:
cfmt = zs cfmt = zs

View file

@ -270,6 +270,8 @@ def parse_ffprobe(txt: str) -> tuple[dict[str, tuple[int, Any]], dict[str, list[
["channel_layout", "chs"], ["channel_layout", "chs"],
["sample_rate", ".hz"], ["sample_rate", ".hz"],
["bit_rate", ".aq"], ["bit_rate", ".aq"],
["bits_per_sample", ".bps"],
["bits_per_raw_sample", ".bprs"],
["duration", ".dur"], ["duration", ".dur"],
] ]

View file

@ -88,7 +88,7 @@ class ThumbCli(object):
if rem.startswith(".hist/th/") and rem.split(".")[-1] in ["webp", "jpg", "png"]: if rem.startswith(".hist/th/") and rem.split(".")[-1] in ["webp", "jpg", "png"]:
return os.path.join(ptop, rem) return os.path.join(ptop, rem)
if fmt[:1] in "jw": if fmt[:1] in "jw" and fmt != "wav":
sfmt = fmt[:1] sfmt = fmt[:1]
if sfmt == "j" and self.args.th_no_jpg: if sfmt == "j" and self.args.th_no_jpg:
@ -129,7 +129,7 @@ class ThumbCli(object):
tpath = thumb_path(histpath, rem, mtime, fmt, self.fmt_ffa) tpath = thumb_path(histpath, rem, mtime, fmt, self.fmt_ffa)
tpaths = [tpath] tpaths = [tpath]
if fmt[:1] == "w": if fmt[:1] == "w" and fmt != "wav":
# also check for jpg (maybe webp is unavailable) # also check for jpg (maybe webp is unavailable)
tpaths.append(tpath.rsplit(".", 1)[0] + ".jpg") tpaths.append(tpath.rsplit(".", 1)[0] + ".jpg")

View file

@ -50,7 +50,7 @@ HAVE_AVIF = False
HAVE_WEBP = False HAVE_WEBP = False
EXTS_TH = set(["jpg", "webp", "png"]) EXTS_TH = set(["jpg", "webp", "png"])
EXTS_AC = set(["opus", "owa", "caf", "mp3"]) EXTS_AC = set(["opus", "owa", "caf", "mp3", "wav"])
EXTS_SPEC_SAFE = set("aif aiff flac mp3 opus wav".split()) EXTS_SPEC_SAFE = set("aif aiff flac mp3 opus wav".split())
PTN_TS = re.compile("^-?[0-9a-f]{8,10}$") PTN_TS = re.compile("^-?[0-9a-f]{8,10}$")
@ -355,8 +355,9 @@ class ThumbSrv(object):
tex = tpath.rsplit(".", 1)[-1] tex = tpath.rsplit(".", 1)[-1]
want_mp3 = tex == "mp3" want_mp3 = tex == "mp3"
want_opus = tex in ("opus", "owa", "caf") want_opus = tex in ("opus", "owa", "caf")
want_wav = tex == "wav"
want_png = tex == "png" want_png = tex == "png"
want_au = want_mp3 or want_opus want_au = want_mp3 or want_opus or want_wav
for lib in self.args.th_dec: for lib in self.args.th_dec:
can_au = lib == "ff" and ( can_au = lib == "ff" and (
ext in self.fmt_ffa or ext in self.fmt_ffv ext in self.fmt_ffa or ext in self.fmt_ffv
@ -371,6 +372,8 @@ class ThumbSrv(object):
funs.append(self.conv_opus) funs.append(self.conv_opus)
elif want_mp3: elif want_mp3:
funs.append(self.conv_mp3) funs.append(self.conv_mp3)
elif want_wav:
funs.append(self.conv_wav)
elif want_png: elif want_png:
funs.append(self.conv_waves) funs.append(self.conv_waves)
png_ok = True png_ok = True
@ -580,7 +583,7 @@ class ThumbSrv(object):
self._run_ff(cmd, vn) self._run_ff(cmd, vn)
def _run_ff(self, cmd: list[bytes], vn: VFS, oom: int = 400) -> None: def _run_ff(self, cmd: list[bytes], vn: VFS, oom: int = 400) -> None:
# self.log((b" ".join(cmd)).decode("utf-8")) self.log((b" ".join(cmd)).decode("utf-8"))
ret, _, serr = runcmd(cmd, timeout=vn.flags["convt"], nice=True, oom=oom) ret, _, serr = runcmd(cmd, timeout=vn.flags["convt"], nice=True, oom=oom)
if not ret: if not ret:
return return
@ -807,6 +810,42 @@ class ThumbSrv(object):
# fmt: on # fmt: on
self._run_ff(cmd, vn, oom=300) self._run_ff(cmd, vn, oom=300)
def conv_wav(self, abspath: str, tpath: str, fmt: str, vn: VFS) -> None:
if self.args.no_acode or not self.args.q_wav:
raise Exception("disabled in server config")
self.wait4ram(0.2, tpath)
tags, rawtags = ffprobe(abspath, int(vn.flags["convt"] / 2))
if "ac" not in tags:
raise Exception("not audio")
bits = tags[".bps"][1]
if bits == 0.0:
bits = tags[".bprs"][1]
codec = b"pcm_s32le"
if bits == 16.0:
codec = b"pcm_s16le"
elif bits == 24.0:
codec = b"pcm_s24le"
self.log("conv2 wav-tmp", 6)
# fmt: off
cmd = [
b"ffmpeg",
b"-nostdin",
b"-v", b"error",
b"-hide_banner",
b"-i", fsenc(abspath),
b"-map", b"0:a:0",
b"-c:a", codec,
fsenc(tpath)
]
# fmt: on
print(cmd)
self._run_ff(cmd, vn, oom=300)
def conv_opus(self, abspath: str, tpath: str, fmt: str, vn: VFS) -> None: def conv_opus(self, abspath: str, tpath: str, fmt: str, vn: VFS) -> None:
if self.args.no_acode or not self.args.q_opus: if self.args.no_acode or not self.args.q_opus:
raise Exception("disabled in server config") raise Exception("disabled in server config")

View file

@ -306,6 +306,7 @@ var Ls = {
"mt_c2owa": "opus-weba, for iOS 17.5 and newer\">owa", "mt_c2owa": "opus-weba, for iOS 17.5 and newer\">owa",
"mt_c2caf": "opus-caf, for iOS 11 through 17\">caf", "mt_c2caf": "opus-caf, for iOS 11 through 17\">caf",
"mt_c2mp3": "use this on very old devices\">mp3", "mt_c2mp3": "use this on very old devices\">mp3",
"mt_c2wav": "use this for uncompressed playback\">wav",
"mt_c2ok": "nice, good choice", "mt_c2ok": "nice, good choice",
"mt_c2nd": "that's not the recommended output format for your device, but that's fine", "mt_c2nd": "that's not the recommended output format for your device, but that's fine",
"mt_c2ng": "your device does not seem to support this output format, but let's try anyways", "mt_c2ng": "your device does not seem to support this output format, but let's try anyways",
@ -6802,6 +6803,7 @@ var mpl = (function () {
'<a href="#" id="ac2owa" class="tgl btn" tt="' + L.mt_c2owa + '</a>' + '<a href="#" id="ac2owa" class="tgl btn" tt="' + L.mt_c2owa + '</a>' +
'<a href="#" id="ac2caf" class="tgl btn" tt="' + L.mt_c2caf + '</a>' + '<a href="#" id="ac2caf" class="tgl btn" tt="' + L.mt_c2caf + '</a>' +
'<a href="#" id="ac2mp3" class="tgl btn" tt="' + L.mt_c2mp3 + '</a>' + '<a href="#" id="ac2mp3" class="tgl btn" tt="' + L.mt_c2mp3 + '</a>' +
'<a href="#" id="ac2wav" class="tgl btn" tt="' + L.mt_c2wav + '</a>' +
'</div></div>' '</div></div>'
) : '') + ) : '') +
@ -6917,8 +6919,10 @@ var mpl = (function () {
if (!have_acode) if (!have_acode)
c = false; c = false;
else if (/\.(wav|flac)$/i.exec(cs)) else if (/\.flac$/i.exec(cs))
c = r.ac_flac; c = r.ac_flac;
else if (/\.wav$/i.exec(cs))
c = r.ac_wav;
else if (/\.(aac|m4a)$/i.exec(cs)) else if (/\.(aac|m4a)$/i.exec(cs))
c = r.ac_aac; c = r.ac_aac;
else if (/\.(oga|ogg|opus)$/i.exec(cs) && (!can_ogg || mpl.ac2 == 'mp3')) else if (/\.(oga|ogg|opus)$/i.exec(cs) && (!can_ogg || mpl.ac2 == 'mp3'))
@ -6942,8 +6946,8 @@ var mpl = (function () {
return; return;
} }
var dv = can_ogg ? 'opus' : can_caf ? 'caf' : 'mp3', var dv = can_ogg ? 'opus' : can_caf ? 'caf' : can_mp3 ? 'mp3' : 'wav',
fmts = ['opus', 'owa', 'caf', 'mp3'], fmts = ['opus', 'owa', 'caf', 'mp3', 'wav'],
btns = []; btns = [];
if (v === dv) if (v === dv)
@ -6953,7 +6957,8 @@ var mpl = (function () {
if ((v == 'opus' && !can_ogg) || if ((v == 'opus' && !can_ogg) ||
(v == 'caf' && !can_caf) || (v == 'caf' && !can_caf) ||
(v == 'owa' && !can_owa)) (v == 'owa' && !can_owa) ||
(v == 'mp3' && !can_mp3))
toast.warn(15, L.mt_c2ng); toast.warn(15, L.mt_c2ng);
if (v == 'owa' && IPHONE) if (v == 'owa' && IPHONE)
@ -7091,11 +7096,13 @@ var mpl = (function () {
var za, var za,
can_ogg = true, can_ogg = true,
can_owa = false, can_owa = false,
can_mp3 = false,
can_caf = APPLE && !/ OS ([1-9]|1[01])_/.test(UA); can_caf = APPLE && !/ OS ([1-9]|1[01])_/.test(UA);
try { try {
za = new Audio(); za = new Audio();
can_ogg = za.canPlayType('audio/ogg; codecs=opus') === 'probably'; can_ogg = za.canPlayType('audio/ogg; codecs=opus') === 'probably';
can_owa = za.canPlayType('audio/webm; codecs=opus') === 'probably'; can_owa = za.canPlayType('audio/webm; codecs=opus') === 'probably';
can_mp3 = za.canPlayType('audio/mpeg') === 'probably';
can_caf = za.canPlayType('audio/x-caf') && can_caf; //'maybe' can_caf = za.canPlayType('audio/x-caf') && can_caf; //'maybe'
} }
catch (ex) { } catch (ex) { }