click^2
Javanaut 1 year ago
parent 6fdcd20176
commit dd8f472ac5

@ -2,11 +2,6 @@
import os, sys, subprocess, json, click, time
from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widgets import Header, Footer, Placeholder
VERSION='0.1.0'
DEFAULT_VIDEO_ENCODER = 'vp9'
@ -51,65 +46,6 @@ STREAM_LAYOUT_STEREO = 'stereo'
STREAM_LAYOUT_6CH = '6ch'
class DashboardScreen(Screen):
def __init__(self):
super().__init__()
context = self.app.getContext()
context['dashboard'] = 'dashboard'
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield Placeholder("Dashboard Screen")
yield Footer()
class SettingsScreen(Screen):
def __init__(self):
super().__init__()
context = self.app.getContext()
def compose(self) -> ComposeResult:
yield Placeholder("Settings Screen")
yield Footer()
class HelpScreen(Screen):
def __init__(self):
super().__init__()
context = self.app.getContext()
def compose(self) -> ComposeResult:
yield Placeholder("Help Screen")
yield Footer()
class ModesApp(App):
BINDINGS = [
("d", "switch_mode('dashboard')", "Dashboard"),
("s", "switch_mode('settings')", "Settings"),
("h", "switch_mode('help')", "Help"),
]
MODES = {
"dashboard": DashboardScreen,
"settings": SettingsScreen,
"help": HelpScreen,
}
def __init__(self, context = {}):
super().__init__()
self.context = context
def on_mount(self) -> None:
self.switch_mode("dashboard")
def getContext(self):
return self.context
def executeProcess(commandSequence):
process = subprocess.Popen(commandSequence, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@ -355,7 +291,7 @@ def streams(filename):
@click.option("-d", "--denoise", is_flag=True, default=False)
def convert(ctx, paths, label, video_encoder, quality, preset, stereo_bitrate, ac3_bitrate, dts_bitrate, crop, clear_metadata, default_subtitle, forced_audio, default_audio, denoise):
def convert(ctx, paths, label, video_encoder, quality, preset, stereo_bitrate, ac3_bitrate, dts_bitrate, crop, clear_metadata, default_subtitle, default_audio, denoise):
"""Batch conversion of audiovideo files in format suitable for web playback, e.g. jellyfin
Files found under PATHS will be converted according to parameters.
@ -363,149 +299,144 @@ def convert(ctx, paths, label, video_encoder, quality, preset, stereo_bitrate, a
Suffices will we appended to filename in case of multiple created files
or if the filename has not changed."""
#startTime = time.perf_counter()
#sourcePath = paths[0]
#targetFilename = paths[1]
startTime = time.perf_counter()
#if not os.path.isfile(sourcePath):
# raise click.ClickException(f"There is no file with path {sourcePath}")
sourcePath = paths[0]
targetFilename = paths[1]
#click.echo(f"src: {sourcePath} tgt: {targetFilename}")
if not os.path.isfile(sourcePath):
raise click.ClickException(f"There is no file with path {sourcePath}")
click.echo(f"src: {sourcePath} tgt: {targetFilename}")
#click.echo(f"ve={video_encoder}")
click.echo(f"ve={video_encoder}")
#qualityTokens = quality.split(',')
#q_list = [q for q in qualityTokens if q.isnumeric()]
qualityTokens = quality.split(',')
#click.echo(q_list)
q_list = [q for q in qualityTokens if q.isnumeric()]
#ctx.obj['bitrates'] = {}
#ctx.obj['bitrates']['stereo'] = str(stereo_bitrate) if str(stereo_bitrate).endswith('k') else f"{stereo_bitrate}k"
#ctx.obj['bitrates']['ac3'] = str(ac3_bitrate) if str(ac3_bitrate).endswith('k') else f"{ac3_bitrate}k"
#ctx.obj['bitrates']['dts'] = str(dts_bitrate) if str(dts_bitrate).endswith('k') else f"{dts_bitrate}k"
click.echo(q_list)
ctx.obj['bitrates'] = {}
ctx.obj['bitrates']['stereo'] = str(stereo_bitrate) if str(stereo_bitrate).endswith('k') else f"{stereo_bitrate}k"
ctx.obj['bitrates']['ac3'] = str(ac3_bitrate) if str(ac3_bitrate).endswith('k') else f"{ac3_bitrate}k"
ctx.obj['bitrates']['dts'] = str(dts_bitrate) if str(dts_bitrate).endswith('k') else f"{dts_bitrate}k"
#click.echo(f"a={ctx.obj['bitrates']['stereo']}")
#click.echo(f"ac3={ctx.obj['bitrates']['ac3']}")
#click.echo(f"dts={ctx.obj['bitrates']['dts']}")
click.echo(f"a={ctx.obj['bitrates']['stereo']}")
click.echo(f"ac3={ctx.obj['bitrates']['ac3']}")
click.echo(f"dts={ctx.obj['bitrates']['dts']}")
#performCrop = (crop != 'none')
performCrop = (crop != 'none')
#if performCrop:
#cropTokens = crop.split(',')
if performCrop:
#if cropTokens and len(cropTokens) == 2:
cropTokens = crop.split(',')
#cropStart, cropLength = crop.split(',')
#else:
#cropStart = DEFAULT_CROP_START
#cropLength = DEFAULT_CROP_LENGTH
#click.echo(f"crop start={cropStart} length={cropLength}")
if cropTokens and len(cropTokens) == 2:
cropStart, cropLength = crop.split(',')
else:
cropStart = DEFAULT_CROP_START
cropLength = DEFAULT_CROP_LENGTH
click.echo(f"crop start={cropStart} length={cropLength}")
#click.echo(f"\nRunning {len(q_list)} jobs")
#streamDescriptor = getStreamDescriptor(sourcePath)
click.echo(f"\nRunning {len(q_list)} jobs")
#commandTokens = COMMAND_TOKENS + [sourcePath]
streamDescriptor = getStreamDescriptor(sourcePath)
#for q in q_list:
commandTokens = COMMAND_TOKENS + [sourcePath]
#click.echo(f"\nRunning job q={q}")
#mappingVideoTokens = ['-map', 'v:0']
#mappingTokens = mappingVideoTokens.copy()
#audioTokens = []
for q in q_list:
#audioIndex = 0
#for audioStreamDescriptor in streamDescriptor:
click.echo(f"\nRunning job q={q}")
#if audioStreamDescriptor['type'] == STREAM_TYPE_AUDIO:
mappingVideoTokens = ['-map', 'v:0']
mappingTokens = mappingVideoTokens.copy()
audioTokens = []
#mappingTokens += ['-map', f"a:{audioIndex}"]
#audioTokens += generateAudioTokens(ctx.obj, audioIndex, audioStreamDescriptor['layout'])
#audioIndex += 1
audioIndex = 0
for audioStreamDescriptor in streamDescriptor:
if audioStreamDescriptor['type'] == STREAM_TYPE_AUDIO:
#for s in range(len([d for d in streamDescriptor if d['type'] == STREAM_TYPE_SUBTITLE])):
#mappingTokens += ['-map', f"s:{s}"]
mappingTokens += ['-map', f"a:{audioIndex}"]
audioTokens += generateAudioTokens(ctx.obj, audioIndex, audioStreamDescriptor['layout'])
audioIndex += 1
for s in range(len([d for d in streamDescriptor if d['type'] == STREAM_TYPE_SUBTITLE])):
mappingTokens += ['-map', f"s:{s}"]
#if video_encoder == 'av1':
#commandSequence = commandTokens + mappingTokens + audioTokens + generateAV1Tokens(q, preset) + audioTokens
#if clear_metadata:
#commandSequence += generateClearTokens(streamDescriptor)
if video_encoder == 'av1':
#if performCrop:
#commandSequence += generateCropTokens(cropStart, cropLength)
commandSequence = commandTokens + mappingTokens + audioTokens + generateAV1Tokens(q, preset) + audioTokens
#commandSequence += generateOutputTokens(targetFilename, DEFAULT_FILE_SUFFIX, q)
if clear_metadata:
commandSequence += generateClearTokens(streamDescriptor)
#click.echo(f"Command: {' '.join(commandSequence)}")
if performCrop:
commandSequence += generateCropTokens(cropStart, cropLength)
#executeProcess(commandSequence)
commandSequence += generateOutputTokens(targetFilename, DEFAULT_FILE_SUFFIX, q)
click.echo(f"Command: {' '.join(commandSequence)}")
#if video_encoder == 'vp9':
executeProcess(commandSequence)
#commandSequence1 = commandTokens + mappingVideoTokens + generateVP9Pass1Tokens(q)
#if performCrop:
# commandSequence1 += generateCropTokens(cropStart, cropLength)
if video_encoder == 'vp9':
#commandSequence1 += NULL_TOKENS
commandSequence1 = commandTokens + mappingVideoTokens + generateVP9Pass1Tokens(q)
#click.echo(f"Command 1: {' '.join(commandSequence1)}")
if performCrop:
commandSequence1 += generateCropTokens(cropStart, cropLength)
#if os.path.exists(TEMP_FILE_NAME):
# os.remove(TEMP_FILE_NAME)
commandSequence1 += NULL_TOKENS
#executeProcess(commandSequence1)
click.echo(f"Command 1: {' '.join(commandSequence1)}")
if os.path.exists(TEMP_FILE_NAME):
os.remove(TEMP_FILE_NAME)
#commandSequence2 = commandTokens + mappingTokens
executeProcess(commandSequence1)
#if denoise:
# commandSequence2 += generateDenoiseTokens()
#commandSequence2 += generateVP9Pass2Tokens(q) + audioTokens
commandSequence2 = commandTokens + mappingTokens
#if clear_metadata:
# commandSequence2 += generateClearTokens(streamDescriptor)
if denoise:
commandSequence2 += generateDenoiseTokens()
#if performCrop:
# commandSequence2 += generateCropTokens(cropStart, cropLength)
commandSequence2 += generateVP9Pass2Tokens(q) + audioTokens
#commandSequence2 += generateOutputTokens(targetFilename, DEFAULT_FILE_SUFFIX, q)
if clear_metadata:
commandSequence2 += generateClearTokens(streamDescriptor)
#click.echo(f"Command 2: {' '.join(commandSequence2)}")
if performCrop:
commandSequence2 += generateCropTokens(cropStart, cropLength)
#executeProcess(commandSequence2)
commandSequence2 += generateOutputTokens(targetFilename, DEFAULT_FILE_SUFFIX, q)
click.echo(f"Command 2: {' '.join(commandSequence2)}")
#click.echo('\nDONE\n')
executeProcess(commandSequence2)
#endTime = time.perf_counter()
#click.echo(f"Time elapsed {endTime - startTime}")
app = ModesApp(ctx.obj)
app.run()
click.echo('\nDONE\n')
click.echo(f"app result: {app.getContext()}")
endTime = time.perf_counter()
click.echo(f"Time elapsed {endTime - startTime}")

Loading…
Cancel
Save