SoFunction
Updated on 2025-04-17

How to accurately locate and modify mvhd atoms of MP4 files in Python

A deep understanding of the structure of MP4 files is very important for safely modifying metadata. MP4 files organize data using a structure based on atoms, each atom represents a specific information or data block. For example, the moov atom contains the metadata information of the video, and the mvhd atom contains the header information of the video, such as time scale, duration, etc. When modifying metadata, we need to accurately locate the corresponding atoms and understand the field structure and data type inside them. This ensures that we do not destroy other important data structures when modifying, thus avoiding file corruption. For example, when modifying the duration, you need to find the duration field in the mvhd atom and ensure that the modified data is formatted correctly and will not affect the parsing of other fields.

Modify the metadata of the video display time

import struct

class ModifyDurationClass:
    def __init__(self, file_path,new_duration):
        self.file_path = file_path
        self.new_duration = new_duration
        self.modify_duration()

    def modify_duration(self):
        with open(self.file_path, 'rb') as f:
            mp4_data = ()

        # Find mvhd atoms        mvhd_offset = mp4_data.find(b'mvhd')
        if mvhd_offset == -1:
            print("Mvhd atom not found")
            return

        # The size of mvhd atom (usually 32 bytes, but may change)        mvhd_size = ('>I', mp4_data[mvhd_offset - 4:mvhd_offset])[0]
        # offset of time scale field (within mvhd atom)        timescale_offset = mvhd_offset + 16
        # The offset of the duration field (inside the mvhd atom)        duration_offset = mvhd_offset + 20

        # Read the current time scale and duration      ...

#User class# file_path = 'input_video.mp4'
# new_duration = 120 # new duration (in seconds)#
# MP4DurationModifier(file_path,new_duration)

Python modify MP4 metadata

In addition to the duration, MP4 metadata can also be modified through Python.

1. Video title and description

Video titles and descriptions are important information for video files, and they can help users understand the content and theme of the video. With Python, we can easily modify this information. For example, using the ffmpeg-python library, you can use the following code to modify the video title and description:

import ffmpeg

input_file = 'input.mp4'
output_file = 'output.mp4'

(input_file).output(output_file, metadata='title=New title', metadata='description=New description').run()

2. Author and copyright information

Authors and copyright information are legal attributes of video files that record the creators and copyright holders of videos. Modifying this information helps protect the copyright of the video and protect the rights and interests of creators. For example, using the moviepy library, you can use the following code to modify the author and copyright information:

from  import VideoFileClip

input_file = 'input.mp4'
output_file = 'output.mp4'

video = VideoFileClip(input_file)
video.write_videofile(output_file, metadata={'author': 'New Author', 'copyright': 'New Copyright Information'})

3. Video size and resolution

While video size and resolution are often closely related to the encoding and compression of the video, in some cases we can change this information by modifying the metadata. For example, the display size of a video can be modified so that it is displayed at different proportions when played. Using the ffmpeg-python library, you can use the following code to modify the video size:

import ffmpeg

input_file = 'input.mp4'
output_file = 'output.mp4'

(input_file).output(output_file, vf='scale=1280:720').run()

4. Encoding parameters

The encoding parameters include the encoding format, bit rate, frame rate, etc. of the video. By modifying these parameters, the compression effect and playback quality of the video can be optimized. For example, you can use the ffmpeg-python library to modify the encoding format and bit rate of a video:

import ffmpeg

input_file = 'input.mp4'
output_file = 'output.mp4'

(input_file).output(output_file, vcodec='libx264', bitrate='1000k').run()

5. Track information

Track information describes the properties of different tracks in the video file, such as audio tracks, video tracks, etc. By modifying track information, you can adjust the priority, language and other attributes of the track. For example, you can use the ffmpeg-python library to modify the language of an audio track:

import ffmpeg

input_file = 'input.mp4'
output_file = 'output.mp4'

(input_file).output(output_file, map_metadata=-1, metadata='language=eng').run()

6. Frame information

Frame information includes the frame type, timestamp, etc. of the video. By modifying the frame information, some special video effects can be achieved, such as adjusting the sequence of frames, inserting specific frames, etc. For example, you can use the moviepy library to modify the frame rate of a video:

from  import VideoFileClip

input_file = 'input.mp4'
output_file = 'output.mp4'

video = VideoFileClip(input_file)
video = video.set_fps(30)  # Modify the frame rate to 30fpsvideo.write_videofile(output_file)

7. Rotation and direction information

The rotation and direction information records the rotation angle and direction of the video, which is particularly important for some videos taken using devices such as mobile phones. By modifying this information, the video can be adjusted to display in the correct direction when playing. For example, you can use the ffmpeg-python library to modify the rotation angle of a video:

import ffmpeg

​​​​​​​input_file = 'input.mp4'
output_file = 'output.mp4'

(input_file).output(output_file, vf='transpose=2').run()  # Rotate 90 degrees

8. Time code

Timecode is information used in video files to identify specific points in time, and it plays an important role in video editing and post-production. By modifying the time code, accurate editing and synchronization of videos can be achieved. For example, you can use the moviepy library to modify the time code of a video:

from  import VideoFileClip

input_file = 'input.mp4'
output_file = 'output.mp4'

video = VideoFileClip(input_file)
video = (10, 20)  # From 10th to 20thvideo.write_videofile(output_file)

9. Subtitle information

Subtitle information is data used in video files to display text information, which can help viewers better understand video content. By modifying the subtitle information, you can add, delete or modify subtitle text, styles, etc. For example, you can use the ffmpeg-python library to add subtitles:

import ffmpeg

input_file = 'input.mp4'
output_file = 'output.mp4'
subtitle_file = ''

(input_file).output(output_file, vf=f'subtitles={subtitle_file}').run()

10. Other custom metadata

In addition to the common metadata mentioned above, MP4 files also support custom metadata fields. These fields can be defined and modified according to specific application scenarios and requirements. For example, some custom metadata for video classification, tagging, rating, etc. can be added. Using the ffmpeg-python library, you can use the following code to add custom metadata:

import ffmpeg

input_file = 'input.mp4'
output_file = 'output.mp4'

(input_file).output(output_file, metadata='

This is the article about how Python accurately locates and modifys mvhd atoms in MP4 files. For more related Python locates and modifys MP4mvhd atoms, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!