CrypticSignal / better-ffmpeg-progress

A Python package that allows you to run an FFmpeg command & see percentage progress, ETA, etc.
https://pypi.org/project/better-ffmpeg-progress
MIT License
54 stars 16 forks source link

Error: UnboundLocalError: cannot access local variable 'progress_bar' where it is not associated with a value #13

Closed AlderStream closed 1 year ago

AlderStream commented 1 year ago

bug description

steps to reproduce

Script

import os
from tqdm import tqdm
from better_ffmpeg_progress import FfmpegProcess

def get_video_files(directory):
    video_extensions = ['.mp4', '.mkv', '.avi', '.mov', '.flv', '.wmv']
    video_files = []

    for foldername, subfolders, filenames in os.walk(directory):
        for filename in filenames:
            if any(filename.lower().endswith(ext) for ext in video_extensions):
                full_path = os.path.join(foldername, filename)
                video_files.append(full_path)

    # sort list
    video_files.sort()

    return video_files

def transcode_to_hevc(input_file, output_file):
    # List of FFmpeg arguments
    command = ["C:/Users/paula/ffmpeg-6.0-essentials_build/bin/ffmpeg.exe", 
               "-i", input_file, 
               "-c:v", "libx265", 
               output_file]

    # Create FfmpegProcess object
    process = FfmpegProcess(command)

    # Run FFmpeg command
    process.run()

if __name__ == "__main__":
    input_directory = 'E:/Novatek/Movie/'  # current directory, you can change this to any directory you want
    output_directory = 'D:/dash cam videos 2023-08-27/transcoded dashcam videos/'  # Ensure this directory exists or change the path

    video_list = get_video_files(input_directory)
    # print(video_list)

    files_to_transcode = video_list # Replace with your list of files

    for file in tqdm(files_to_transcode):
        print(file)
        output_file = output_directory + file.split('/')[-1].replace('.MP4', '_hevc.mp4')
        transcode_to_hevc(file, output_file)

Error


File "<python path>", line 123, in run
PS <working directory> &<python path> <script path>
  0%|                                                                                 | 0/1176 [00:00<?, ?it/s]E:/Novatek/Movie/2022_0831_122548_212.MP4
Running: C:/Users/paula/ffmpeg-6.0-essentials_build/bin/ffmpeg.exe -y -i E:/Novatek/Movie/2022_0831_122548_212.MP4 -c:v libx265 D:/dash cam videos 2023-08-27/transcoded dashcam videos/2022_0831_122548_212_hevc.mp4 -hide_banner -loglevel verbose
  0%|                                                                                 | 0/1176 [00:21<?, ?it/s]
Traceback (most recent call last):
  File "C:\Users\paula\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\better_ffmpeg_progress\better_ffmpeg_progress.py", line 66, in run
    while process.poll() is None:
          ^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1236, in poll
    return self._internal_poll()
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1264.0_x64__qbz5n2kfra8p0\Lib\subprocess.py", line 1575, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\paula\python-learning\transcode-videos.py", line 47, in <module>
    transcode_to_hevc(file, output_file)
  File "c:\Users\paula\python-learning\transcode-videos.py", line 33, in transcode_to_hevc
    process.run()
  File "C:\Users\paula\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\better_ffmpeg_progress\better_ffmpeg_progress.py", line 117, in run
    progress_bar.close()
    ^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'progress_bar' where it is not associated with a value```
CrypticSignal commented 1 year ago

@PaulAlexAhlstrom I have refactored the code and released v2.1.1 which should eradicate this issue. Please update to the latest version with pip3 install better-ffmpeg-progress --upgrade and report back.