Get "Media Created" Timestamp With Python For .mp4 And .m4a Video, Audio Files (no EXIF)
Get "Media created" timestamp with Python for .mp4 and .m4a video, audio files (no EXIF)
In this article, we will explore how to get the "Media created" timestamp for .mp4 and .m4a video, audio files using Python. This timestamp is not available in the EXIF data, but rather in the media file itself. We will use the ffmpeg-python
library to achieve this.
What is ffmpeg-python?
ffmpeg-python
is a Python wrapper for the popular ffmpeg
command-line tool. ffmpeg
is a powerful multimedia framework that can be used for a wide range of tasks, including video and audio processing, encoding, and decoding. The ffmpeg-python
library provides a Pythonic interface to ffmpeg
, making it easy to use in Python scripts.
Installing ffmpeg-python
To use ffmpeg-python
, you need to have ffmpeg
installed on your system. You can download the latest version of ffmpeg
from the official website. Once you have ffmpeg
installed, you can install ffmpeg-python
using pip:
pip install ffmpeg-python
Getting the "Media created" timestamp
To get the "Media created" timestamp, we can use the ffprobe
command-line tool, which is part of the ffmpeg
package. ffprobe
is a multimedia stream analyzer that can be used to extract information from media files.
Here is an example of how to use ffprobe
to get the "Media created" timestamp:
import ffmpeg

stream = ffmpeg.input('input.mp4')
metadata = stream.metadata
creation_date = metadata.get('creation_time')
print(creation_date)
However, this will not work for .m4a files. To get the "Media created" timestamp for .m4a files, we need to use a different approach.
Getting the "Media created" timestamp for .m4a files
To get the "Media created" timestamp for .m4a files, we can use the ffprobe
command-line tool with the -show_entries
option. This option allows us to specify the metadata that we want to extract from the media file.
Here is an example of how to use ffprobe
to get the "Media created" timestamp for .m4a files:
import subprocess
output = subprocess.check_output(['ffprobe', '-v', 'error', '-show_entries', 'format=creation_time', '-of', 'default=noprint_wrappers=1:nokey=1', 'input.m4a'])
creation_date = output.decode('utf-8').strip()
print(creation_date)
Inserting the "Media created" timestamp as the "Last modified date"
Once we have the "Media created" timestamp, we can insert it as the "Last modified date" using the os
module.
Here is an example of how to insert the "Media created" timestamp as the "Last modified date":
import os
creation_date = '2022-01-01 12:00:00'
os.utime('input.mp4', (os.path.getatime('input.mp4'), os.path.getmtime('input.mp4') + 1))
In this article, we have explored how to get the "Media created" timestamp for .mp4 and .m4a video, audio files using Python. We have used the ffmpeg-python
library to achieve this. We have also shown how to insert the "Media created" timestamp as the "Last modified date" using the os
module.
Example Use Cases
- Media Management: You can use this code to manage your media files by getting the "Media created" timestamp and inserting it as the "Last modified date".
- Data Analysis: You can use this code to analyze your media files by getting the "Media created" timestamp and using it in your data analysis pipeline.
- Automation: You can use this code to automate tasks related to media files by getting the "Media created" timestamp and using it to perform tasks such as renaming files or moving files to a specific directory.
- Support for more file formats: Currently, this code only supports .mp4 and .m4a file formats. In the future, we can add support for more file formats.
- Improved error handling: Currently, this code does not handle errors well. In the future, we can improve error handling to make the code more robust.
- Optimization: Currently, this code is not optimized for performance. In the future, we can optimize the code to make it faster and more efficient.
Q&A: Get "Media created" timestamp with Python for .mp4 and .m4a video, audio files (no EXIF)
Q: What is the "Media created" timestamp?
A: The "Media created" timestamp is the date and time when a media file was created. This timestamp is not available in the EXIF data, but rather in the media file itself.
Q: Why do I need to get the "Media created" timestamp?
A: You may need to get the "Media created" timestamp for various reasons, such as:
- Media management: You can use the "Media created" timestamp to manage your media files, such as renaming files or moving files to a specific directory.
- Data analysis: You can use the "Media created" timestamp to analyze your media files, such as tracking the creation date of files or identifying files that were created on a specific date.
- Automation: You can use the "Media created" timestamp to automate tasks related to media files, such as renaming files or moving files to a specific directory.
Q: How do I get the "Media created" timestamp for .mp4 files?
A: To get the "Media created" timestamp for .mp4 files, you can use the ffmpeg-python
library. Here is an example of how to use ffmpeg-python
to get the "Media created" timestamp:
import ffmpeg
stream = ffmpeg.input('input.mp4')
metadata = stream.metadata
creation_date = metadata.get('creation_time')
print(creation_date)
Q: How do I get the "Media created" timestamp for .m4a files?
A: To get the "Media created" timestamp for .m4a files, you can use the ffprobe
command-line tool with the -show_entries
option. Here is an example of how to use ffprobe
to get the "Media created" timestamp:
import subprocess
output = subprocess.check_output(['ffprobe', '-v', 'error', '-show_entries', 'format=creation_time', '-of', 'default=noprint_wrappers=1:nokey=1', 'input.m4a'])
creation_date = output.decode('utf-8').strip()
print(creation_date)
Q: How do I insert the "Media created" timestamp as the "Last modified date"?
A: To insert the "Media created" timestamp as the "Last modified date", you can use the os
module. Here is an example of how to insert the "Media created" timestamp as the "Last modified date":
import os
creation_date = '2022-01-01 12:00:00'
os.utime('input.mp4', (os.path.getatime('input.mp4'), os.path.getmtime('input.mp4') + 1))
Q: What are the system requirements for this code?
A: The system requirements for this code are* Python 3.x: You need to have Python 3.x installed on your system.
- ffmpeg-python: You need to have the
ffmpeg-python
library installed on your system. - ffprobe: You need to have the
ffprobe
command-line tool installed on your system.
Q: Can I use this code on Windows?
A: Yes, you can use this code on Windows. However, you need to make sure that you have the ffmpeg-python
library and the ffprobe
command-line tool installed on your system.
Q: Can I use this code on macOS?
A: Yes, you can use this code on macOS. However, you need to make sure that you have the ffmpeg-python
library and the ffprobe
command-line tool installed on your system.
Q: Can I use this code on Linux?
A: Yes, you can use this code on Linux. However, you need to make sure that you have the ffmpeg-python
library and the ffprobe
command-line tool installed on your system.