99 lines
5.4 KiB
Python
99 lines
5.4 KiB
Python
# Description: Utility functions for the media validation service
|
|
# Art.c.hive 2024/09/30
|
|
|
|
# Standard libs
|
|
import logging
|
|
import os
|
|
# import logging_config
|
|
|
|
|
|
# result, message = check_video_info(audio_json_content)
|
|
def check_video_info(media_info):
|
|
logging.info("Checking video info...")
|
|
logging.info(f"Media info: {media_info}")
|
|
try:
|
|
#('mediainfo', {}).get('media', {}).get('track', [])
|
|
# Check if the file name ends with .mov
|
|
file_name = media_info.get('media', {}).get('@ref', '')
|
|
logging.info(f"File name in JSON: {file_name}")
|
|
# Determine the parent directory (one level above the basename).
|
|
# Example: for 'SOME/FOLDER/filename.mov' -> parent_dir == 'FOLDER'
|
|
parent_dir = os.path.basename(os.path.dirname(file_name))
|
|
logging.info(f"Parent directory: {parent_dir}")
|
|
|
|
# If the parent directory is 'FILE' accept multiple container types
|
|
if parent_dir.lower() == 'file':
|
|
# Accept .mov, .avi, .m4v, .mp4, .mxf, .mpg (case-insensitive)
|
|
if not any(file_name.lower().endswith(ext) for ext in ('.mov', '.avi', '.m4v', '.mp4', '.mxf', '.mpg', '.mpeg')):
|
|
return False, "The file is not a .mov, .avi, .m4v, .mp4, .mxf, .mpg or .mpeg file."
|
|
|
|
# Map file extensions to lists of acceptable general formats (video)
|
|
general_formats = {
|
|
'.avi': ['AVI'], # General/Format for AVI files
|
|
'.mov': ['QuickTime', 'MOV', 'MPEG-4'], # MediaInfo may report QuickTime or MOV for .mov
|
|
'.mp4': ['MPEG-4', 'MP4', 'QuickTime'], # MPEG-4 container (QuickTime variant ?? VO-MP4-16028_H264.mp4)
|
|
'.m4v': ['MPEG-4', 'MP4'], # MPEG-4 container (Apple variant)
|
|
'.mxf': ['MXF'], # Material eXchange Format
|
|
'.mpg': ['MPEG','MPEG-PS'], # MPEG program/transport streams
|
|
'.mpeg': ['MPEG','MPEG-PS'],
|
|
}
|
|
|
|
# check that the extension correspond to one of the allowed formats in track 0 in the corresponding json file
|
|
file_ext = os.path.splitext(file_name)[1].lower()
|
|
logging.info(f"File extension: {file_ext}")
|
|
expected_formats = general_formats.get(file_ext)
|
|
logging.info(f"Expected formats for extension {file_ext}: {expected_formats}")
|
|
if not expected_formats:
|
|
return False, f"Unsupported file extension: {file_ext}"
|
|
tracks = media_info.get('media', {}).get('track', [])
|
|
if len(tracks) > 0:
|
|
track_0 = tracks[0] # Assuming track 0 is the first element (index 0)
|
|
logging.info(f"Track 0: {track_0}")
|
|
actual_format = track_0.get('Format', '')
|
|
if track_0.get('@type', '') == 'General' and actual_format in expected_formats:
|
|
logging.info(f"File extension {file_ext} matches one of the expected formats {expected_formats} (actual: {actual_format}).")
|
|
else:
|
|
return False, f"Track 0 format '{actual_format}' does not match any expected formats {expected_formats} for extension {file_ext}."
|
|
else:
|
|
# Outside FILE/ directory require .mov specifically
|
|
if not file_name.lower().endswith('.mov'):
|
|
return False, "The file is not a .mov file."
|
|
# Check if track 1's format is ProRes
|
|
tracks = media_info.get('media', {}).get('track', [])
|
|
if len(tracks) > 1:
|
|
track_1 = tracks[1] # Assuming track 1 is the second element (index 1)
|
|
logging.info(f"Track 1: {track_1}")
|
|
if track_1.get('@type', '') == 'Video' and track_1.get('Format', '') == 'ProRes' and track_1.get('Format_Profile', '') == '4444':
|
|
return True, "The file is a .mov file with ProRes format in track 1."
|
|
else:
|
|
return False, "Track 1 format is not ProRes."
|
|
else:
|
|
return False, "No track 1 found."
|
|
|
|
return True, "The file passed the video format checks."
|
|
except Exception as e:
|
|
return False, f"Error processing the content: {e}"
|
|
|
|
# result, message = check_audio_info(json_content)
|
|
def check_audio_info(media_info):
|
|
try:
|
|
# Check if the file name ends with .wav
|
|
file_name = media_info.get('media', {}).get('@ref', '')
|
|
if not file_name.endswith('.wav'):
|
|
logging.info(f"File name in JSON: {file_name}")
|
|
return False, "The file is not a .wav file."
|
|
|
|
# Check if track 1's format is Wave
|
|
tracks = media_info.get('media', {}).get('track', [])
|
|
# Ensure there are at least two track entries before accessing index 1
|
|
if len(tracks) > 1:
|
|
track_1 = tracks[1] # Assuming track 1 is the second element (index 1)
|
|
if track_1.get('@type', '') == 'Audio' and track_1.get('Format', '') == 'PCM' and track_1.get('SamplingRate', '') == '96000' and track_1.get('BitDepth', '') == '24':
|
|
return True, "The file is a .wav file with Wave format in track 1."
|
|
else:
|
|
return False, f"Track 1 format is not Wave. Format: {track_1.get('Format', '')}, SamplingRate: {track_1.get('SamplingRate', '')}, BitDepth: {track_1.get('BitDepth', '')}"
|
|
|
|
return False, "No track 1 found."
|
|
|
|
except Exception as e:
|
|
return False, f"Error processing the content: {e}" |