fromMiniDV/convert_a_file.py

134 lines
6.2 KiB
Python
Raw Normal View History

2025-03-20 19:51:57 +01:00
#!/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}%')