feat: output directory path by output flag
BREAKING-CHANGE: output flag use now directory path instead of output file
This commit is contained in:
parent
9d4f4319c2
commit
2de5044dfe
@ -83,13 +83,13 @@ def get_video_metadata(file_path):
|
|||||||
print(f"❌ Error processing {file_path}: {str(e)}")
|
print(f"❌ Error processing {file_path}: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def process_file(file_path, output_file=None):
|
def process_file(file_path, output_dir=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_dir (str, optional): Directory where the output JSON file will be saved
|
||||||
"""
|
"""
|
||||||
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}")
|
||||||
@ -103,27 +103,34 @@ def process_file(file_path, output_file=None):
|
|||||||
metadata = get_video_metadata(file_path)
|
metadata = get_video_metadata(file_path)
|
||||||
|
|
||||||
if metadata:
|
if metadata:
|
||||||
if not output_file:
|
# Generate output filename based on input file
|
||||||
# Generate output filename based on input file
|
filename = os.path.basename(os.path.splitext(file_path)[0]) + "_metadata.json"
|
||||||
|
|
||||||
|
if output_dir:
|
||||||
|
# Ensure output directory exists
|
||||||
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
|
output_path = os.path.join(output_dir, filename)
|
||||||
|
else:
|
||||||
|
# If no output directory specified, save in the same directory as the 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_path = 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_path, '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_path}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def process_directory(directory_path, output_file=None):
|
def process_directory(directory_path, output_dir=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_dir (str, optional): Directory where the output JSON file will be saved
|
||||||
"""
|
"""
|
||||||
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}")
|
||||||
@ -149,31 +156,38 @@ def process_directory(directory_path, output_file=None):
|
|||||||
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:
|
# 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))
|
filename = f"{dir_name}_metadata.json"
|
||||||
output_file = f"{dir_name}_metadata.json"
|
|
||||||
|
if output_dir:
|
||||||
|
# Ensure output directory exists
|
||||||
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
|
output_path = os.path.join(output_dir, filename)
|
||||||
|
else:
|
||||||
|
# If no output directory specified, save in the current directory
|
||||||
|
output_path = filename
|
||||||
|
|
||||||
# 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_path, '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_path}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="Extract metadata from video files and save as JSON.")
|
parser = argparse.ArgumentParser(description="Extract metadata from video files and save as JSON.")
|
||||||
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="Directory path where output JSON files will be saved")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
input_path = args.input
|
input_path = args.input
|
||||||
output_file = args.output
|
output_dir = 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_dir)
|
||||||
elif os.path.isdir(input_path):
|
elif os.path.isdir(input_path):
|
||||||
process_directory(input_path, output_file)
|
process_directory(input_path, output_dir)
|
||||||
else:
|
else:
|
||||||
print(f"❌ Path not found: {input_path}")
|
print(f"❌ Path not found: {input_path}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user