如何通过Python修改视频文件的creation_time元数据以满足Google Photos上传要求
Got it, let's break down why your current code isn't working and how to actually modify the video file's metadata.
The Problem with Your Current Approach
When you use ffmpeg.probe(), you're just loading the video's metadata into a Python dictionary stored in memory. Changing values in that dictionary doesn't affect the actual video file on your disk at all—it's like editing a copy of a document and expecting the original to update automatically. To make persistent changes, you need to use FFmpeg's encoding/writing capabilities to overwrite the metadata in the file itself.
The Correct Method
We'll use FFmpeg's metadata option to set the creation_time directly on the video stream, and use the codec='copy' flag to avoid re-encoding the video (this keeps the process fast and preserves quality).
Here's a working code example:
import ffmpeg input_path = r"test.MP4" output_path = r"test_updated.MP4" # Use a new file first to avoid accidental data loss new_creation_time = '2015-11-01T20:07:09.000000Z' try: # Build the FFmpeg command: input file, set video stream metadata, copy all streams ( ffmpeg .input(input_path) .output( output_path, # Target the first video stream to set creation_time **{'metadata:s:v:0': f'creation_time={new_creation_time}'}, codec='copy' # Critical: avoids re-encoding, saves time/quality ) .overwrite_output() # Overwrite the output file if it already exists .run(capture_stdout=True, capture_stderr=True) ) # Verify the change worked updated_metadata = ffmpeg.probe(output_path) print(f"Updated creation_time: {updated_metadata['streams'][0]['tags']['creation_time']}") except Exception as e: print(f"Error modifying metadata: {e}")
Key Notes:
codec='copy': This tells FFmpeg to copy the video/audio streams directly without re-encoding. Skipping this would force a full re-encode, which is slow and unnecessary for just changing metadata.- Targeting the Video Stream: The
metadata:s:v:0parameter specifies we're modifying the first video stream's metadata. If your video has multiple streams, adjust the index as needed. - Backup First: Always test with a new output file before overwriting your original video. Once you confirm the metadata is correct, you can replace the original if needed (just set
output_path = input_pathand ensureoverwrite_output()is enabled). - Google Photos Compatibility: This method modifies the actual video file's embedded metadata (not just Windows file properties), which is exactly what Google Photos uses to determine the upload date—so this should work just like your manual Windows property edits.
内容的提问来源于stack exchange,提问作者Anton Bohatov

