xconv: Merge and extend audiobook profiles

- Both profiles are now in the same file
- default to .opus files
- have an option to use .ogg for Android
- The from_chapters profile can now ignore chapter end marks
- from_chapters will add the input filename to the output filename by default
master
Taeyeon Mori 7 years ago
parent 2b963e7b0f
commit bd500baf98
  1. 102
      lib/python/xconv/profiles/audiobook.py
  2. 45
      lib/python/xconv/profiles/audiobook_from_chapters.py

@ -25,40 +25,104 @@ xconv ffmpeg wrapper based on AdvancedAV
Opus Audiobook profile
"""
import os
from ..profile import *
abdefines = dict(
stereo="Use two channels at 48k",
bitrate="Use custom target bitrate",
fancy="Use 56kbps stereo (For dramatic audiobooks with a lot of music and effects)"
bitrate = "Use custom target bitrate",
stereo = "Use 2 channels (Ignored for mono source streams)",
fancy = "Use higher bitrates (48k mono/64k stereo)",
ogg = "Use the .ogg file extension (Currently required on Android)"
)
def apply(stream, defines):
stream.set(codec="libopus",
vbr="on",
b="40k",
ac="1",
application="voip")
if stream.source.channels > 1:
if "stereo" in defines:
stream.set(ac="2",
b="48k")
def apply_stream(stream, defines):
""" Apply the audiobook profile to an output stream """
stream.codec = "libopus"
stream.channels = 1
stream.bitrate = 40_000
stream.set(vbr="on", application="voip")
# High Quality
if "fancy" in defines:
stream.bitrate = 48_000
stream.set(application="audio")
# Stereo Options
if stream.source.channels > 1 and "stereo" in defines:
stream.channels = 2
stream.bitrate = 48_000
if "fancy" in defines:
stream.set(ac="2",
b="56k",
application="audio")
stream.bitrate = 64_000
# Custom bitrate
if "bitrate" in defines:
stream.bitrate = defines["bitrate"]
# At most input bitrate; we wouldn't gain anything since opus should be same or better compression
# Limit to input bitrate
stream.bitrate = min(stream.bitrate, stream.source.bitrate)
@profile
@description("Encode Opus Audiobook")
@output(container="ogg", ext="ogg")
@output(container="ogg", ext="opus")
@defines(**abdefines)
@singleaudio
def audiobook(task, stream, defines):
apply(task.map_stream(stream), defines)
if "ogg" in "defines":
task.change_format(ext="ogg")
apply_stream(task.map_stream(stream), defines)
return True
@profile
@description("Split & Encode Opus Audiobook from M4B chapters")
@output(container="ogg", ext="opus")
@features(no_single_output=True)
@defines(ignore_ends="Ignore chapter end marks and continue until next chapter starts",
chapter_only_names="Don't include the input filename in the output filename",
**abdefines)
@singleaudio
def from_chapters(task, stream, defines):
# Read chapters from input
if "ignore_ends" in defines:
# Make sure nothing is cut out because of
# broken chapter (end) markers
it = iter(task.iter_chapters())
first_chapter = next(it)
chapters = [{"title": first_chapter.title}]
for chapter in it:
chapters[-1]["to"] = chapter.start_time
chapters.append({"ss": chapter.start_time,
"title": chapter.title})
else:
chapters = [{"ss": chapter.start_time,
"to": chapter.end_time,
"title": chapter.title}
for chapter in task.iter_chapters()]
# Output filenames
ext = "ogg" if "ogg" in defines else "opus"
if "chapter_only_names" in defines:
fn_template = os.path.join(task.output_directory, "%%s.%s" % ext)
else:
fn_template = "%s - %%s.%s" % (task.output_prefix, ext)
# Set up output files
for chapter in chapters:
out = task.add_output(fn_template % chapter.pop("title"), "ogg")
out.set(**chapter)
apply_stream(out.map_stream(stream), defines)
return True

@ -1,45 +0,0 @@
#!/usr/bin/env python3
"""
xconv ffmpeg wrapper based on AdvancedAV
-----------------------------------------------------------
AdvancedAV helps with constructing FFmpeg commandline arguments.
It can automatically parse input files with the help of FFmpeg's ffprobe tool (WiP)
and allows programatically mapping streams to output files and setting metadata on them.
-----------------------------------------------------------
Copyright (c) 2015-2017 Taeyeon Mori
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------
Opus Audiobook profile from m4b chapters
"""
from ..profile import *
from .audiobook import apply, abdefines
@profile
@description("Split & Encode Opus Audiobook from M4B chapters")
@output(container="ogg", ext="ogg")
@features(no_single_output=True)
@defines(**abdefines)
@singleaudio
def audiobook_from_chapters(task, stream, defines):
for chapter in task.iter_chapters():
apply(
task.add_output(chapter.title + ".ogg", "ogg")
.set(ss=chapter.start_time,
to=chapter.end_time)
.map_stream(stream), defines)
return True
Loading…
Cancel
Save