Skip to content

Commit

Permalink
desktop.media.tomkv: add --rm-list-regen option, use tmp files for ff…
Browse files Browse the repository at this point in the history
…mpeg output
  • Loading branch information
mk-fg committed Nov 20, 2024
1 parent 753e9c7 commit f55e555
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions desktop/media/tomkv
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ def main(args=None):
is larger than source*factor, otherwise destination.
Intended use is to make an easy-to-use list of files to
rm when replacing old ones with converted versions,
without unnecessary replacement if there's not enough benefit.'''))
without unnecessary replacement if there's not enough benefit.
File is always overwritten when used.'''))
parser.add_argument('--rm-list-regen', action='store_true', help=dd('''
When using -s/--skip-n or similar options,
still check file sizes when they exist, and put them on the list.'''))
parser.add_argument('-s', '--skip-n', metavar='n', type=int, help=dd('''
Skip first N files that'd have been processed otherwise.
Can be used to resume a long operation, using number from
Expand Down Expand Up @@ -189,7 +193,7 @@ def main(args=None):
for err in errs: print(f' {err_verdict()} {err.get("t") or "-"} :: {err.msg}')
else: print(f'\n{dst} :: {err_verdict()} {err.get("t") or "-"} :: {err.msg}')
if skip: continue
src_list[n], p.src, p.dst = p, src, dst
src_list[n], p.src, p.dst, p.tmp = p, src, dst, f'_tmp.{dst}'

# Main ffmpeg conversion loop
dry_run, src_list = not opts.convert, list(filter(None, src_list))
Expand All @@ -211,10 +215,10 @@ def main(args=None):
'pan=stereo|c0=0.5*c2+0.707*c0+0.707*c4+0.5*c3'
'|c1=0.5*c2+0.707*c1+0.707*c5+0.5*c3,volume=2.0' ] + ac
elif p.a.chans != 2: ac = ['-ac', '2'] + ac
movflags = ( ['-movflags', '+faststart']
mov = ( ['-movflags', '+faststart']
if p.dst.rsplit('.', 1)[-1].lower() in ['mp4', 'mov', 'm4v'] else [] )
cmd = [ 'ffmpeg', '-hide_banner', '-i', str(p.src), *filters,
*'-c:v libsvtav1 -preset 5 -crf 38'.split(), *movflags, *ac, '-y', p.dst ]
*'-c:v libsvtav1 -preset 5 -crf 38'.split(), *mov, *ac, '-y', p.tmp ]
dt, ts1 = time.strftime('%Y-%m-%d %H:%M:%S'), time.monotonic()
msg = f'\n\n- {dt} --- [ {n} / {m} ] :: {td_repr(p.td)} :: {p.src} -> {p.dst}\n'
if n <= nx: continue
Expand All @@ -225,19 +229,26 @@ def main(args=None):

sp.run( cmd, check=True,
env=dict(os.environ, SVT_LOG='2'), stdin=sp.DEVNULL )
os.rename(p.tmp, p.dst)

# Per-file stats, compression check, rm-list export
# One-time stats/rm-list catch-up for skipped existing file pairs
if nx and (nd := n-nx) == 1:
for nc, pc in enumerate(src_list, 1): # add skipped dst-files into stats
for nc, pc in enumerate(src_list, 1):
if nc == n: break
try: sz_dst_done += os.stat(pc.dst).st_size; sz_src_done += pc.sz
except: pass
try: sz_dst = os.stat(pc.dst).st_size
except FileNotFoundError: continue
sz_dst_done += sz_dst; sz_src_done += (sz_src := pc.sz)
if rm_list and opts.rm_list_regen:
improved = sz_dst/sz_src < rm_list_factor
print(f'{pc.src if improved else pc.dst}\n', file=rm_list)

# Stats/rm-list for last processed file
target = ''
sz_src_done += (sz_src := p.sz); sz_src_proc += sz_src
sz_dst_done += (sz_dst := os.stat(p.dst).st_size)
if rm_list:
improved = sz_dst/sz_src < rm_list_factor
rm_list.write(f'{p.src if improved else p.dst}\n'); rm_list.flush()
print(f'{p.src if improved else p.dst}\n', file=rm_list, flush=True)
if rm_list_factor is not math.inf:
target = 'better' if improved else 'WORSE'
target = f' [ {target} than {round(rm_list_factor*100)}% target ]'
Expand Down

0 comments on commit f55e555

Please sign in to comment.