feat: adding title
This commit is contained in:
parent
8a407a186a
commit
f856d414f1
@ -10,10 +10,10 @@ SUPPORTED_EXTENSIONS = (".mp4", ".mkv", ".mov", ".avi")
|
|||||||
def get_video_metadata(file_path):
|
def get_video_metadata(file_path):
|
||||||
"""
|
"""
|
||||||
Extract metadata from a video file using ffprobe.
|
Extract metadata from a video file using ffprobe.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
file_path (str): Path to the video file
|
file_path (str): Path to the video file
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: Metadata information
|
dict: Metadata information
|
||||||
"""
|
"""
|
||||||
@ -22,29 +22,31 @@ def get_video_metadata(file_path):
|
|||||||
"ffprobe", "-v", "quiet", "-print_format", "json",
|
"ffprobe", "-v", "quiet", "-print_format", "json",
|
||||||
"-show_format", "-show_streams", file_path
|
"-show_format", "-show_streams", file_path
|
||||||
]
|
]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
print(f"❌ Error processing {file_path}: {result.stderr}")
|
print(f"❌ Error processing {file_path}: {result.stderr}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
data = json.loads(result.stdout)
|
data = json.loads(result.stdout)
|
||||||
|
|
||||||
# Extract filename
|
# Extract filename and title
|
||||||
filename = os.path.basename(file_path)
|
filename = os.path.basename(file_path)
|
||||||
|
title = data.get("format", {}).get("tags", {}).get("title", filename)
|
||||||
|
|
||||||
# Initialize metadata structure
|
# Initialize metadata structure
|
||||||
metadata = {
|
metadata = {
|
||||||
"filename": filename,
|
"filename": filename,
|
||||||
|
"title": title,
|
||||||
"audio_tracks": [],
|
"audio_tracks": [],
|
||||||
"subtitle_tracks": []
|
"subtitle_tracks": []
|
||||||
}
|
}
|
||||||
|
|
||||||
# Process streams
|
# Process streams
|
||||||
for stream in data.get("streams", []):
|
for stream in data.get("streams", []):
|
||||||
codec_type = stream.get("codec_type")
|
codec_type = stream.get("codec_type")
|
||||||
|
|
||||||
if codec_type == "audio":
|
if codec_type == "audio":
|
||||||
track = {
|
track = {
|
||||||
"index": stream.get("index"),
|
"index": stream.get("index"),
|
||||||
@ -63,7 +65,7 @@ def get_video_metadata(file_path):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata["audio_tracks"].append(track)
|
metadata["audio_tracks"].append(track)
|
||||||
|
|
||||||
elif codec_type == "subtitle":
|
elif codec_type == "subtitle":
|
||||||
track = {
|
track = {
|
||||||
"index": stream.get("index"),
|
"index": stream.get("index"),
|
||||||
@ -81,9 +83,9 @@ def get_video_metadata(file_path):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata["subtitle_tracks"].append(track)
|
metadata["subtitle_tracks"].append(track)
|
||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"❌ Error processing {file_path}: {str(e)}")
|
print(f"❌ Error processing {file_path}: {str(e)}")
|
||||||
return None
|
return None
|
||||||
@ -91,7 +93,7 @@ def get_video_metadata(file_path):
|
|||||||
def process_file(file_path, output_file=None):
|
def process_file(file_path, output_file=None):
|
||||||
"""
|
"""
|
||||||
Process a single video file and write metadata to JSON.
|
Process a single video file and write metadata to JSON.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
file_path (str): Path to the video file
|
file_path (str): Path to the video file
|
||||||
output_file (str, optional): Path to output JSON file
|
output_file (str, optional): Path to output JSON file
|
||||||
@ -99,33 +101,33 @@ def process_file(file_path, output_file=None):
|
|||||||
if not os.path.isfile(file_path):
|
if not os.path.isfile(file_path):
|
||||||
print(f"❌ File not found: {file_path}")
|
print(f"❌ File not found: {file_path}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not file_path.lower().endswith(SUPPORTED_EXTENSIONS):
|
if not file_path.lower().endswith(SUPPORTED_EXTENSIONS):
|
||||||
print(f"❌ Unsupported file format: {file_path}")
|
print(f"❌ Unsupported file format: {file_path}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
print(f"📊 Extracting metadata from {os.path.basename(file_path)}")
|
print(f"📊 Extracting metadata from {os.path.basename(file_path)}")
|
||||||
metadata = get_video_metadata(file_path)
|
metadata = get_video_metadata(file_path)
|
||||||
|
|
||||||
if metadata:
|
if metadata:
|
||||||
if not output_file:
|
if not output_file:
|
||||||
# Generate output filename based on input file
|
# Generate output filename based on input file
|
||||||
base_name = os.path.splitext(file_path)[0]
|
base_name = os.path.splitext(file_path)[0]
|
||||||
output_file = f"{base_name}_metadata.json"
|
output_file = f"{base_name}_metadata.json"
|
||||||
|
|
||||||
# Write metadata to JSON file
|
# Write metadata to JSON file
|
||||||
with open(output_file, 'w', encoding='utf-8') as f:
|
with open(output_file, 'w', encoding='utf-8') as f:
|
||||||
json.dump(metadata, f, indent=2, ensure_ascii=False)
|
json.dump(metadata, f, indent=2, ensure_ascii=False)
|
||||||
|
|
||||||
print(f"✅ Metadata saved to {output_file}")
|
print(f"✅ Metadata saved to {output_file}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def process_directory(directory_path, output_file=None):
|
def process_directory(directory_path, output_file=None):
|
||||||
"""
|
"""
|
||||||
Process all video files in a directory and write metadata to JSON.
|
Process all video files in a directory and write metadata to JSON.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
directory_path (str): Path to the directory
|
directory_path (str): Path to the directory
|
||||||
output_file (str, optional): Path to output JSON file
|
output_file (str, optional): Path to output JSON file
|
||||||
@ -133,36 +135,36 @@ def process_directory(directory_path, output_file=None):
|
|||||||
if not os.path.isdir(directory_path):
|
if not os.path.isdir(directory_path):
|
||||||
print(f"❌ Directory not found: {directory_path}")
|
print(f"❌ Directory not found: {directory_path}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
all_metadata = {}
|
all_metadata = {}
|
||||||
file_count = 0
|
file_count = 0
|
||||||
|
|
||||||
for root, _, files in os.walk(directory_path):
|
for root, _, files in os.walk(directory_path):
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.lower().endswith(SUPPORTED_EXTENSIONS):
|
if file.lower().endswith(SUPPORTED_EXTENSIONS):
|
||||||
file_path = os.path.join(root, file)
|
file_path = os.path.join(root, file)
|
||||||
print(f"📊 Extracting metadata from {file}")
|
print(f"📊 Extracting metadata from {file}")
|
||||||
metadata = get_video_metadata(file_path)
|
metadata = get_video_metadata(file_path)
|
||||||
|
|
||||||
if metadata:
|
if metadata:
|
||||||
# Use relative path as key
|
# Use relative path as key
|
||||||
rel_path = os.path.relpath(file_path, directory_path)
|
rel_path = os.path.relpath(file_path, directory_path)
|
||||||
all_metadata[rel_path] = metadata
|
all_metadata[rel_path] = metadata
|
||||||
file_count += 1
|
file_count += 1
|
||||||
|
|
||||||
if file_count == 0:
|
if file_count == 0:
|
||||||
print(f"❌ No supported video files found in {directory_path}")
|
print(f"❌ No supported video files found in {directory_path}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not output_file:
|
if not output_file:
|
||||||
# Generate output filename based on directory name
|
# Generate output filename based on directory name
|
||||||
dir_name = os.path.basename(os.path.normpath(directory_path))
|
dir_name = os.path.basename(os.path.normpath(directory_path))
|
||||||
output_file = f"{dir_name}_metadata.json"
|
output_file = f"{dir_name}_metadata.json"
|
||||||
|
|
||||||
# Write all metadata to a single JSON file
|
# Write all metadata to a single JSON file
|
||||||
with open(output_file, 'w', encoding='utf-8') as f:
|
with open(output_file, 'w', encoding='utf-8') as f:
|
||||||
json.dump(all_metadata, f, indent=2, ensure_ascii=False)
|
json.dump(all_metadata, f, indent=2, ensure_ascii=False)
|
||||||
|
|
||||||
print(f"✅ Metadata for {file_count} files saved to {output_file}")
|
print(f"✅ Metadata for {file_count} files saved to {output_file}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -171,10 +173,10 @@ def main():
|
|||||||
parser.add_argument("input", help="Path to input video file or directory")
|
parser.add_argument("input", help="Path to input video file or directory")
|
||||||
parser.add_argument("-o", "--output", help="Path to output JSON file")
|
parser.add_argument("-o", "--output", help="Path to output JSON file")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
input_path = args.input
|
input_path = args.input
|
||||||
output_file = args.output
|
output_file = args.output
|
||||||
|
|
||||||
if os.path.isfile(input_path):
|
if os.path.isfile(input_path):
|
||||||
process_file(input_path, output_file)
|
process_file(input_path, output_file)
|
||||||
elif os.path.isdir(input_path):
|
elif os.path.isdir(input_path):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user