def process_single_video()

in finealignment/video_alignment.py [0:0]


def process_single_video(video_id, worker_number):
    s3_folder_videos = 'videos/'
    video_key = f'{s3_folder_videos}/{video_id}.mp4'
    video_filename = f'{video_id}.mp4'
    video_local_path = os.path.join(video_folder_path, video_filename)
    if result_exists(video_filename,output_folder_path):
        print(f"Skipping {video_filename}, result already exists.")
        return

    # Download video from S3
    if not download_video_from_s3(video_key, video_local_path):
        # Handle download failure
        error_data = {"error": "File not found in S3"}
        error_file_path = os.path.join(output_folder_path, f"errors_{video_id}.json")
        with open(error_file_path, "w") as f:
            json.dump(error_data, f, indent=4)
        
        # Update status report for download failure
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        status_report = f"{timestamp} - {video_id} - failed - File not found in S3\n"
        print(status_report)
        with open(f"status/status_alignment_{worker_number}.txt", "a") as f:
            f.write(status_report)
        
        return

    # Construct paths
    json_path = os.path.join(json_folder_path, f"{video_id}.json")
    json_result_path = os.path.join(output_folder_path, f"{video_id}.json")

    # Load JSON file
    with open(json_path, 'r') as json_file:
        video_data = json.load(json_file)

    try:
        # Adjust scene boundaries using PySceneDetect to determine FPS
        fps, adjusted_scenes = adjust_scene_boundaries(video_local_path, video_data['scenes'], video_id, str(worker_number))

        # Update scenes in the original data
        video_data['scenes'] = adjusted_scenes
        video_data['fps'] = fps

        # Update all timestamps to FrameTimecode format
        video_data = update_timestamps_in_json(video_data, fps, video_id, str(worker_number))

        # Write updated JSON back to file
        with open(json_result_path, 'w') as json_file:
            json.dump(video_data, json_file, indent=4)

        print(f"Processed video {video_id}.")

        # Prepare the status report
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        status_report = f"{timestamp} - {video_id} - complete\n"
        print(status_report)
        
        # Append the status report to status.txt
        if worker_number is None:
            with open("status_alignment.txt", "a") as f:
                f.write(status_report)
        else:    
            with open(f"status/status_alignment_{worker_number}.txt", "a") as f:
                f.write(status_report)

    except Exception as e:
        # Handle any errors in adjusting scenes or updating timestamps
        error_data = {
            "error": str(e),
            "video_id": video_id,
            "worker_number": worker_number
        }
        error_file_path = os.path.join(output_folder_path, f"errors_{video_id}.json")
        with open(error_file_path, "w") as f:
            json.dump(error_data, f, indent=4)

        # Update status report for failure
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        status_report = f"{timestamp} - {video_id} - failed - Error during processing: {str(e)}\n"
        print(status_report)
        with open(f"status/status_alignment_{worker_number}.txt", "a") as f:
            f.write(status_report)

    finally:
        # Remove the video file after processing, even if an error occurred
        if os.path.exists(video_local_path):
            os.remove(video_local_path)
            print(f"Deleted local file {video_local_path} after processing.")