I'm trying to refactor this two similar functions that I've extracted from the video-diet code. I think the best thing to do would be to create a File
class and put the path, type, codec and file manager inside. I'm having difficulty creating the class. I want to indicate in the class that the manager can be None
for the audio files.
How would you refactor the two functions into one?
def process_audio(audio, errors_files):
if get_codec(str(audio)) == 'hevc':
typer.secho(f'Skipping: {audio}')
return
typer.secho(f'Processing: {audio}')
new_path = destination(audio, True)
if new_path.exists():
os.remove(str(new_path))
try:
convert_file(str(audio), str(new_path))
os.remove(str(audio))
if audio.suffix == new_path.suffix:
shutil.move(new_path, str(audio))
except ffmpeg._run.Error:
typer.secho(f'ffmpeg could not process this file: {str(audio)}', fg=RED)
errors_files.append(audio)
def process_video(errors_files, manager, video):
if get_codec(str(video)) != 'hevc':
typer.secho(f'Skipping: {video}')
return
typer.secho(f'Processing: {video}')
new_path = destination(video, False)
if new_path.exists():
os.remove(str(new_path))
try:
convert_video_progress_bar(str(video), str(new_path), manager)
os.remove(str(video))
if video.suffix == new_path.suffix:
shutil.move(new_path, str(video))
except ffmpeg._run.Error:
typer.secho(f'ffmpeg could not process: {str(video)}', fg=RED)
errors_files.append(video)