Initial commit

This commit is contained in:
Davide De Nardis 2025-03-20 19:51:57 +01:00
commit 8c472f2e5c
3 changed files with 299 additions and 0 deletions

100
collect_durations.py Normal file
View file

@ -0,0 +1,100 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
# pip install ffmpeg-python
import ffmpeg
from pathlib import Path
def available_files(folder: str | Path | None = None, starts_w: str | None = None,
contains: str | list[str] | tuple[str] | None = None,
suffix: str = '.log'):
"""
generates all the available filenames
"""
if folder is None:
folder = Path('.')
else:
if isinstance(folder, str):
folder = Path(folder)
if not isinstance(folder, Path):
raise ValueError(f'Wrong type for folder: expected str | Path | None, found {type(folder)}')
if not folder.is_dir():
raise ValueError(f'folder parameter must point to a directory: {folder}')
for f in folder.iterdir():
if f.is_file() and f.suffix == suffix:
if starts_w is not None and not f.name.startswith(starts_w):
continue
if contains is not None:
if isinstance(contains, str):
# a single string
if contains not in f.name:
continue
if isinstance(contains, list):
keep_it = False
for con in contains:
if con in f.name:
keep_it = True
break
if not keep_it:
continue
yield f
def get_duration(file_path):
probe = ffmpeg.probe(file_path)
return float(probe['format']['duration'])
def main():
pass
# PS C:\DV Capture\My Video\Video> history
#
# Id CommandLine
# -- -----------
# 1 dir
# 2 ffmpeg -i '.\My Video 20090106-15.05.15.avi' -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
# 3 ffmpeg -i '.`My Video 20100106-15.05.15.avi' -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
# 4 ffmpeg -i ".\My Video 20100106-15.05.15.avi" -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
# 5 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
# 6 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -vf idet -frames:v 500 -an -f null -
# 7 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -vf "bwdif" -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output_d.mp4
# 8 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -vf "bwdif" -c:v libx265 -crf 22 -c:a aac -b:a 192k output_d2.mp4
# 9 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i ".\My Video 20100106-15.04.41.avi" -vf "yadif_cuda=1" -c:v hevc_nvenc -preset slow -cq 22 -c:a aac -b:a 192k output_d3...
# 10 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i ".\My Video 20100106-15.04.41.avi" -vf "format=nv12, yadif_cuda=1" -c:v hevc_nvenc -preset slow -cq 22 -c:a aac -b:a 1...
# 11 history
# 12 nvidia-smi
# 13 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -vf "yadif_cuda=1" -c:v hevc_nvenc -preset slow -cq 22 -c:a aac -b:a 192k output_d3.mp4
# 14 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i ".\My Video 20100106-15.04.41.avi" -vf "bwdif" -c:v hevc_nvenc -preset slow -cq 22 -c:a aac -b:a 192k output_d3.mp4
if __name__ == '__main__':
# main()
# base_folder = Path('C:/DV Capture/My Video/Video')
base_folder = Path('C:/Hmc/DV_out/C02_')
total_duration = 0
for f in available_files(base_folder, suffix='.avi'):
prb = ffmpeg.probe(f)
streams = prb.get('streams')
frmt = prb.get('format')
dur = float(frmt.get('duration'))
frc = 1000 * (dur - int(dur))
total_duration += dur
human_dur = time.strftime('%H:%M:%S', time.gmtime(dur))
print(f'------------------ {f} {human_dur}.{frc:03.0f} {dur}')
# print(f'Streams: {streams}')
# for n, stream in enumerate(streams):
# print(f'S:{n}')
# for k, v in stream.items():
# print(f' {k}: {v}')
# print('Format:')
# for k, v in frmt.items():
# print(f'{k}: {v}')
# break
frc = 1000 * (total_duration - int(total_duration))
human_t_dur = time.strftime('%H:%M:%S', time.gmtime(total_duration))
print(f'Totale: {human_t_dur}.{frc:03.0f} {total_duration}')

133
convert_a_file.py Normal file
View file

@ -0,0 +1,133 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import subprocess
# pip install ffmpeg-python
import ffmpeg
from pathlib import Path
def run_command(command):
# Run the command and capture the output
try:
result = subprocess.run(command, shell=True, check=True, text=True, capture_output=True)
# Print the standard output and error
# print("Standard Output:\n", result.stdout)
# print("Standard Error:\n", result.stderr)
return result.stdout # Return stdout if you want to use it elsewhere
except subprocess.CalledProcessError as e:
print(f"Command failed with error: {e.stderr}")
return None
def available_files(folder: str | Path | None = None, starts_w: str | None = None,
contains: str | list[str] | tuple[str] | None = None,
suffix: str = '.log'):
"""
generates all the available filenames
"""
if folder is None:
folder = Path('.')
else:
if isinstance(folder, str):
folder = Path(folder)
if not isinstance(folder, Path):
raise ValueError(f'Wrong type for folder: expected str | Path | None, found {type(folder)}')
if not folder.is_dir():
raise ValueError(f'folder parameter must point to a directory: {folder}')
for f in folder.iterdir():
if f.is_file() and f.suffix == suffix:
if starts_w is not None and not f.name.startswith(starts_w):
continue
if contains is not None:
if isinstance(contains, str):
# a single string
if contains not in f.name:
continue
if isinstance(contains, list):
keep_it = False
for con in contains:
if con in f.name:
keep_it = True
break
if not keep_it:
continue
yield f
def get_duration(file_path):
probe = ffmpeg.probe(file_path)
return float(probe['format']['duration'])
#
# Id CommandLine
# -- -----------
# 1 dir
# 2 ffmpeg -i '.\My Video 20090106-15.05.15.avi' -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
# 3 ffmpeg -i '.`My Video 20100106-15.05.15.avi' -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
# 4 ffmpeg -i ".\My Video 20100106-15.05.15.avi" -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
# 5 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
# 6 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -vf idet -frames:v 500 -an -f null -
# 7 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -vf "bwdif" -c:v libx265 -crf 22 -preset slow -c:a aac -b:a 192k output_d.mp4
# 8 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -vf "bwdif" -c:v libx265 -crf 22 -c:a aac -b:a 192k output_d2.mp4
# 9 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i ".\My Video 20100106-15.04.41.avi" -vf "yadif_cuda=1" -c:v hevc_nvenc -preset slow -cq 22 -c:a aac -b:a 192k output_d3...
# 10 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i ".\My Video 20100106-15.04.41.avi" -vf "format=nv12, yadif_cuda=1" -c:v hevc_nvenc -preset slow -cq 22 -c:a aac -b:a 1...
# 11 history
# 12 nvidia-smi
# 13 ffmpeg -i ".\My Video 20100106-15.04.41.avi" -vf "yadif_cuda=1" -c:v hevc_nvenc -preset slow -cq 22 -c:a aac -b:a 192k output_d3.mp4
# 14 ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i ".\My Video 20100106-15.04.41.avi" -vf "bwdif" -c:v hevc_nvenc -preset slow -cq 22 -c:a aac -b:a 192k output_d3.mp4
def convert_a_file(in_file: Path) -> (Path, str):
base_stem = in_file.stem
tape_name, ts, secs = base_stem.split('.')
if secs != '00':
raise ValueError(f'Error Secs !=00 file:{in_file}')
dt, tm = ts.split('_')
yy, mm, dd = [int(v) for v in dt.split('-')]
HH, MM, SS = [int(v) for v in tm.split('-')]
yy += 2000
human_ts = f'{yy}_{mm:02}_{dd:02}_{HH:02}{MM:02}{SS:02}'
out_file = in_file.parent / f'{tape_name}_{human_ts}.mp4'
if in_file.is_file() and not out_file.is_file():
cmd = f'ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i "{in_file}" -vf "bwdif" -c:v hevc_nvenc -preset slow -cq 21 -c:a aac -b:a 192k {out_file}'
print(f'We Do process: {cmd}')
return out_file, run_command(cmd)
return out_file, f'Skipped file {in_file}'
if __name__ == '__main__':
# main()
# base_folder = Path('C:/DV Capture/My Video/Video')
t0 = time.time()
# base_folder = Path('C:/Hmc/DV_out/C02_')
base_folder = Path('C:/Hmc/DV_out/T01 ')
total_duration = 0
tot_size_in = 0
tot_size_out = 0
for f in available_files(base_folder, suffix='.avi'):
# if f.stem != 'c02.11-03-27_17-11-54.00':
# continue
stat = f.stat()
input_size = stat.st_size//1024
print(f'Input size is: {input_size}')
ti = time.time()
f_out, stdout = convert_a_file(f)
ti = time.time() - ti
human_dur = time.strftime('%H:%M:%S', time.gmtime(ti))
if f_out.is_file():
stat_o = f_out.stat()
output_size = stat_o.st_size // 1024
tot_size_in += input_size
tot_size_out += output_size
print(f'Size Ratio: {100 * output_size / input_size:.2f}% conversion time: {ti} {human_dur}\n{stdout}')
# break
now = time.time()
duration = now - t0
human_dur = time.strftime('%H:%M:%S', time.gmtime(duration))
print(f'Total duration: {duration:.0f} {human_dur}')
print(f'Totals')
print(f'size in:{tot_size_in} size out: {tot_size_out} ratio {100 * tot_size_out / tot_size_in:.2f}%')

66
extract_apks.py Normal file
View file

@ -0,0 +1,66 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
from pathlib import Path
import time
APK_TOOLS_FOLDER = Path('C:/Users/main/AppData/Local/Android/Sdk/platform-tools')
DESTINATION = Path('C:/Mc/Aws/APKs/FromPy')
DESTINATION.mkdir(exist_ok=True, parents=True)
# it.beghelli.dome
# .\adb.exe shell pm list packages -3
# .\adb.exe shell pm list packages -3 > x5_find_l3_2025_02_09.txt
# .\adb.exe shell dumpsys activity recents
# .\adb.exe shell pm path it.beghelli.dome
# .\adb.exe pull /data/app/~~ImzbTI0j21ORlO9dusKcAg==/it.beghelli.dome-dtycHO3eKSqdeUqiMRedDw==/base.apk dome.apk
# .\adb.exe install .\dome.apk
def run_command(command):
# Run the command and capture the output
try:
result = subprocess.run(command, shell=True, check=True, text=True, capture_output=True)
# Print the standard output and error
# print("Standard Output:\n", result.stdout)
# print("Standard Error:\n", result.stderr)
return result.stdout # Return stdout if you want to use it elsewhere
except subprocess.CalledProcessError as e:
print(f"Command failed with error: {e.stderr}")
return None
def do_adb(params):
tool = APK_TOOLS_FOLDER / 'adb.exe'
cmd = f'{tool} {params}'
return run_command(cmd)
def find_apks_files(package_name):
r = do_adb(f' shell pm path {package_name}')
return r.split('\n')
if __name__ == '__main__':
# r = do_adb('devices')
# print(r)
#
# r = do_adb(' shell pm list packages -3')
# for line in r.split('\n'):
# if 'translate' in line.lower():
# print(f'-{line}')
#
str_now = time.strftime('%Y_%m_%d_%H%M%S')
pkg_name = 'com.google.android.apps.translate'
dest_folder = DESTINATION / f'{pkg_name.replace(".", "_")}_{str_now}'
dest_folder.mkdir(parents=True, exist_ok=True)
for f in find_apks_files(pkg_name):
if len(f) > 8:
source = f[8:]
src = Path(source)
print(src.name)
print(source, dest_folder)
do_adb(f'pull {source} {dest_folder}')