CN114374875A - MP4 file recording method and device and readable storage medium - Google Patents

MP4 file recording method and device and readable storage medium Download PDF

Info

Publication number
CN114374875A
CN114374875A CN202111664254.XA CN202111664254A CN114374875A CN 114374875 A CN114374875 A CN 114374875A CN 202111664254 A CN202111664254 A CN 202111664254A CN 114374875 A CN114374875 A CN 114374875A
Authority
CN
China
Prior art keywords
index
file
index table
recording
field
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202111664254.XA
Other languages
Chinese (zh)
Inventor
蔡焱钢
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhejiang Dahua Technology Co Ltd
Original Assignee
Zhejiang Dahua Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Zhejiang Dahua Technology Co Ltd filed Critical Zhejiang Dahua Technology Co Ltd
Priority to CN202111664254.XA priority Critical patent/CN114374875A/en
Publication of CN114374875A publication Critical patent/CN114374875A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04NPICTORIAL COMMUNICATION, e.g. TELEVISION
    • H04N21/00Selective content distribution, e.g. interactive television or video on demand [VOD]
    • H04N21/40Client devices specifically adapted for the reception of or interaction with content, e.g. set-top-box [STB]; Operations thereof
    • H04N21/43Processing of content or additional data, e.g. demultiplexing additional data from a digital video stream; Elementary client operations, e.g. monitoring of home network or synchronising decoder's clock; Client middleware
    • H04N21/44Processing of video elementary streams, e.g. splicing a video clip retrieved from local storage with an incoming video stream, rendering scenes according to MPEG-4 scene graphs
    • H04N21/4402Processing of video elementary streams, e.g. splicing a video clip retrieved from local storage with an incoming video stream, rendering scenes according to MPEG-4 scene graphs involving reformatting operations of video signals for household redistribution, storage or real-time display
    • H04N21/440218Processing of video elementary streams, e.g. splicing a video clip retrieved from local storage with an incoming video stream, rendering scenes according to MPEG-4 scene graphs involving reformatting operations of video signals for household redistribution, storage or real-time display by transcoding between formats or standards, e.g. from MPEG-2 to MPEG-4
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/60Information retrieval; Database structures therefor; File system structures therefor of audio data
    • G06F16/61Indexing; Data structures therefor; Storage structures
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/70Information retrieval; Database structures therefor; File system structures therefor of video data
    • G06F16/71Indexing; Data structures therefor; Storage structures
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04NPICTORIAL COMMUNICATION, e.g. TELEVISION
    • H04N21/00Selective content distribution, e.g. interactive television or video on demand [VOD]
    • H04N21/40Client devices specifically adapted for the reception of or interaction with content, e.g. set-top-box [STB]; Operations thereof
    • H04N21/43Processing of content or additional data, e.g. demultiplexing additional data from a digital video stream; Elementary client operations, e.g. monitoring of home network or synchronising decoder's clock; Client middleware
    • H04N21/433Content storage operation, e.g. storage operation in response to a pause request, caching operations
    • H04N21/4334Recording operations
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04NPICTORIAL COMMUNICATION, e.g. TELEVISION
    • H04N21/00Selective content distribution, e.g. interactive television or video on demand [VOD]
    • H04N21/40Client devices specifically adapted for the reception of or interaction with content, e.g. set-top-box [STB]; Operations thereof
    • H04N21/43Processing of content or additional data, e.g. demultiplexing additional data from a digital video stream; Elementary client operations, e.g. monitoring of home network or synchronising decoder's clock; Client middleware
    • H04N21/439Processing of audio elementary streams
    • H04N21/4398Processing of audio elementary streams involving reformatting operations of audio signals

Abstract

The invention provides a method and a device for recording an MP4 file and a readable storage medium, wherein the recording method comprises the following steps: creating an empty MP4 file; when recording audio and video media data of a current frame, only storing the index of the current frame in a memory, and writing the index of the current frame to the tail position of an index table related to a target moov field in the MP4 file, wherein the target moov field comprises at least four index tables and a free box field for storing newly added indexes of each index table, and each index table is respectively used for storing different index information contents of the corresponding frame; and writing the audio and video media data corresponding to the current frame into the MP4 file. The method and the device are used for reducing memory occupation while considering the effectiveness of the MP4 file and high recording efficiency after power failure, thereby improving the recording effect of the MP4 file.

Description

MP4 file recording method and device and readable storage medium
Technical Field
The invention relates to the technical field of video recording, in particular to a method and a device for recording an MP4 file and a readable storage medium.
Background
In the prior art, audio and video media data is often recorded in MP4 file package format. In the recording process, two processing modes are generally adopted for the moov field, wherein the first processing mode is that the whole moov field containing indexes of all frames in a memory is written into an MP4 file every time one frame of data is recorded; the second processing mode is to write the moov field in the memory into the MP4 file after all the frames are recorded.
The first processing method may cause recording inefficiency because moov field data may be much larger than frame data. The second approach, while improving recording efficiency, can result in the MP4 file being invalid after power down. In addition, whether the first processing mode or the second processing mode is adopted, the problem of overlarge memory occupation still exists.
Therefore, how to improve the recording effect of the MP4 file becomes a technical problem to be solved urgently.
Disclosure of Invention
The invention provides a method and a device for recording an MP4 file and a readable storage medium, which are used for reducing the memory occupation while considering the effectiveness and high recording efficiency of the MP4 file after power failure, thereby improving the recording effect of the MP4 file.
In a first aspect, an embodiment of the present invention provides a method for recording an MP4 file, including:
creating an empty MP4 file;
when recording audio and video media data of a current frame, only storing the index of the current frame in a memory, and writing the index of the current frame to the tail position of an index table related to a target moov field in the MP4 file, wherein the target moov field comprises at least four index tables and a free box field for storing newly added indexes of each index table, and each index table is respectively used for storing different index information contents of the corresponding frame;
and writing the audio and video media data corresponding to the current frame into the MP4 file.
In one possible implementation manner, before the writing the index of the current frame to the end position of the index table related to the target moov field in the MP4 file, the method further includes:
setting a header space of the MP4 file, wherein the header space is used for storing a written preset moov field;
after recording audio and video media data with a first preset frame number according to the preset moov field, determining the space occupied by each index table in the memory, wherein the first preset frame number is an integer greater than 1;
dividing the head space according to the space size of the memory occupied by each index table, and determining the position and size of each index table in the head space;
and updating the preset moov field to the target moov field.
In one possible implementation manner, the segmenting the head space according to the size of the space occupied by each index table in the memory, and determining the position and size of each index table in the head space includes:
determining the ratio of the space size occupied by each index table to the total space size occupied by the at least four index tables according to the space size occupied by each index table to the memory;
and dividing the head space according to the ratio, and determining the maximum length of each index table and the corresponding free box field and the position of the corresponding table header in the MP4 file.
In one possible implementation manner, in the process of recording the audio/video media data of the first preset frame number according to the preset moov field, the method further includes:
and storing the index of each frame in the audio and video media data with the first preset frame number in the memory, and writing the indexes of all frames corresponding to the audio and video media data with the first preset frame number into the MP4 file.
In one possible implementation manner, after the creating an empty MP4 file, the method further includes:
and recording and storing the corresponding frame index information content of the stco index table in the at least four index tables every second preset frame number.
In one possible implementation manner, after writing the index of the current frame to an end position of an index table related to a target moov field in the MP4 file, the method further includes:
and writing fixed fields except the index tables in the target moov fields into the MP4 file.
In a second aspect, an embodiment of the present invention further provides an apparatus for recording an MP4 file, including:
a creation unit: for creating an empty MP4 file;
the first processing unit is configured to, when recording audio and video media data of a current frame, only store an index of the current frame in a memory, and write the index of the current frame to an end position of an index table in the MP4 file, where the index table is associated with a target moov field, the target moov field includes at least four index tables and a free box field reserved for each index table, and each index table is used to store different index information contents of a corresponding frame;
and the second processing unit is used for writing the audio and video media data corresponding to the current frame into the MP4 file.
In one possible implementation manner, before the writing of the index of the current frame to the end position of the index table in the MP4 file related to the target moov field, the recording apparatus further includes a third processing unit, and the third processing unit is configured to:
setting a header space of the MP4 file, wherein the header space is used for storing a written preset moov field;
after recording audio and video media data with a first preset frame number according to the preset moov field, determining the space occupied by each index table in the memory, wherein the first preset frame number is an integer greater than 1;
and dividing the head space according to the space size of the memory occupied by each index table, and determining the position and the size of each index table in the head space.
In one possible implementation manner, the third processing unit is configured to:
determining the ratio of the space size occupied by each index table to the total space size occupied by the at least four index tables according to the space size occupied by each index table to the memory;
and dividing the head space according to the ratio, and determining the maximum length of each index table and the corresponding free box field and the position of the corresponding table header in the MP4 file.
In one possible implementation manner, in the process of recording the audio/video media data of the first preset frame number according to the preset moov field, the third processing unit is further configured to:
and storing the index of each frame in the audio and video media data with the first preset frame number in the memory, and writing the indexes of all frames corresponding to the audio and video media data with the first preset frame number into the MP4 file.
In one possible implementation manner, after the creating of an empty MP4 file, the recording apparatus further includes a recording unit, where the recording unit is configured to:
and recording and storing the corresponding frame index information content of the stco index table in the at least four index tables every second preset frame number.
In one possible implementation manner, after writing the index of the current frame to the end position of the index table in the MP4 file related to the target moov field, the recording apparatus further includes a fourth processing unit, and the fourth processing unit is configured to:
and writing fixed fields except the index tables in the target moov fields into the MP4 file.
In a third aspect, an embodiment of the present invention further provides a recording apparatus, where the recording apparatus includes a processor, and the processor is configured to implement, when executing a computer program stored in a memory, the steps of the method for recording an MP4 file according to any one of the above descriptions.
In a fourth aspect, the present invention further provides a readable storage medium, on which a computer program is stored, where the computer program, when executed by a processor, implements the steps of the recording method of the MP4 file as described in any one of the above.
The invention has the following beneficial effects:
the invention provides a method, a device and a readable storage medium for recording an MP4 file, firstly, an empty MP4 file is created, when audio and video media data of a current frame are recorded, only the index of the current frame is stored in a memory, and the index of the current frame is written to the tail position of an index table related to a target moov field in the MP4 file, namely, in the recording process, only the index of the current frame needs to be stored in the memory, thereby effectively reducing the memory occupation, in addition, the target moov field comprises at least four index tables used for storing different index information contents of corresponding frames, and also comprises a free box field used for storing newly added indexes of each index table, therefore, when the audio and video media data of one frame are recorded, the target moov field is updated, thereby ensuring the MP4 file to be effective after power failure, and only the recorded index of the current frame needs to be written to the tail position of the index table related to the target moov field in the MP4 file, the written data is less, and only the index of the current frame in the target moov field needs to be updated, so that the recording efficiency is improved. After the audio-video media data corresponding to the current frame is written into the MP4 file, a recorded MP4 file is obtained. Therefore, after the power is off, the MP4 file is effective, the recording efficiency is high, the memory occupation is reduced, and the recording effect of the MP4 file is improved.
Drawings
Fig. 1 is a schematic structural diagram of one of moov fields used for recording MP4 files in the related art;
fig. 2 is a flowchart of a method for recording an MP4 file according to an embodiment of the present invention;
fig. 3 is a schematic structural diagram of an MP4 file in the MP4 file recording method according to an embodiment of the present invention;
fig. 4 is a schematic structural diagram of a free box field arranged behind an stts index table in the recording method of an MP4 file according to the embodiment of the present invention;
fig. 5 is a flowchart of a method before step S102 in a method for recording an MP4 file according to an embodiment of the present invention;
fig. 6 is a flowchart of a method in step S203 of a method for recording an MP4 file according to an embodiment of the present invention;
fig. 7 is a flowchart of a method for recording an MP4 file according to an embodiment of the present invention;
fig. 8 is a flowchart of a method for recording an MP4 file by a terminal according to an embodiment of the present invention;
fig. 9 is a schematic structural diagram of a recording apparatus for MP4 files according to an embodiment of the present invention.
Detailed Description
The terms "first," "second," and the like in the description and claims of the present invention and in the above-described drawings are used for distinguishing between different objects and not for describing a particular order. Furthermore, the terms "comprises" and any variations thereof, are intended to cover non-exclusive inclusions. For example, a process, method, system, article, or apparatus that comprises a list of steps or elements is not limited to only those steps or elements listed, but may alternatively include other steps or elements not listed, or inherent to such process, method, article, or apparatus.
Reference herein to "an embodiment" means that a particular feature, structure, or characteristic described in connection with the embodiment can be included in at least one embodiment of the invention. The appearances of the phrase in various places in the specification are not necessarily all referring to the same embodiment, nor are separate or alternative embodiments mutually exclusive of other embodiments. It is explicitly and implicitly understood by one skilled in the art that the embodiments described herein can be combined with other embodiments.
In order to better understand the technical solutions of the present invention, the technical solutions of the present invention are described in detail below with reference to the drawings and the specific embodiments, and it should be understood that the specific features in the embodiments and the embodiments of the present invention are detailed descriptions of the technical solutions of the present invention, and are not limitations of the technical solutions of the present invention, and the technical features in the embodiments and the embodiments of the present invention may be combined with each other without conflict.
In the related art, the recording of MP4 file usually adopts moov field structure as shown in fig. 1, where the index information of the code stream is all put in the stbl table, and the table includes four index tables of stts, stss, stsz and stco related to the index. The four index tables are next to each other in the video file, so that when the stts index table is lengthened, the stts index table is inevitably damaged, accordingly, the start position of the stts index table is changed, and the whole stts index table is rewritten in the file. In order to ensure that the flushed index table can be recovered when the moov field is refreshed, the index information of all frames needs to be stored in the memory, and thus, the technical problem of excessive memory occupation is caused. In addition, in order to ensure that the file is valid after power-off, the entire moov field in the memory is written into the MP4 file every time one frame of data is recorded, thereby reducing the recording efficiency.
In view of this, embodiments of the present invention provide a method, an apparatus, and a readable storage medium for recording an MP4 file, so as to reduce memory usage and improve the recording effect of an MP4 file while considering that an MP4 file is effective and recording efficiency is high after power off.
The embodiment of the present invention provides a method for recording an MP4 file, which may be applied to terminals such as a capture camera, a Video camera, a Digital camera, a tv card, a Digital Video Recorder (DVR), etc., but is not limited to the mentioned terminals, and may select a corresponding terminal to record according to actual application requirements, and is not limited herein.
As shown in fig. 2, the recording method includes:
s101: creating an empty MP4 file;
in the implementation, a recording instruction is received, and an empty MP4 file is created.
S102: when recording audio and video media data of a current frame, only storing the index of the current frame in a memory, and writing the index of the current frame to the tail position of an index table related to a target moov field in the MP4 file, wherein the target moov field comprises at least four index tables and a free box field for storing newly added indexes of each index table, and each index table is respectively used for storing different index information contents of the corresponding frame;
in the specific implementation process, when recording the audio and video media data of the current frame, only the index of the current frame is stored in the memory, so that only the index of the current frame needs to be stored in the memory in the recording process, thereby effectively reducing the memory occupation. In addition, the index of the current frame needs to be written to the tail position of the index table related to the target moov field in the MP4 file, the written data is less, and only the index of the current frame in the moov field needs to be updated, so that the recording efficiency is improved. In addition, the target moov field comprises at least four index tables for storing different index information contents of corresponding frames, and also comprises a free box field for storing newly added indexes of each index table, so that the target moov field is updated every time one frame of audio and video media data is recorded, and the MP4 file is enabled after power failure.
It should be noted that, if only a video frame exists in the recorded audio/video media data, the target moov field includes four index tables; if the recorded audio and video media data has audio frames besides video frames, the target moov field comprises eight index tables. Fig. 3 is a schematic structural diagram of an MP4 file when only video frames exist, in which, a target moov field in the MP4 file includes four index tables including stts, stss, stsz and stco, and accordingly, the target moov field includes four free box fields for storing new indexes of the index tables, and in combination with the structural diagram shown in fig. 3, a free box field is set behind the four index tables including stts, stss, stsz and stco. The stts index table is used for storing the time of the corresponding frame, the stss index table is used for marking the key frame, and the stts index table can be implemented by storing the frame number of the I frame in the video frame, the stsz index table is used for storing the length of the corresponding frame, and the stco index table is used for storing the position of the corresponding frame. In addition, for the structure in the structure diagram that is not related to the index, the arrangement in the related art can be specifically referred to, and is not described in detail here.
Fig. 4 is a schematic structural diagram of one of the structures where a free box field is set behind an stss index table, and as shown in fig. 3 and fig. 4, the start positions of the stts, stss, stsz, and scto index tables are set to be fixed, that is, the head positions of the respective index tables are fixed, and as the number of recording frames increases, the stss index table becomes longer, and by adjusting the size of the free box field behind the stss index table, the free box field behind the stss index table becomes shorter accordingly, so that the stss index table behind the stss index table can be prevented from being damaged by flushing. In this way, the previous index table information does not need to be saved, only the index of the current frame needs to be saved in the memory, and the index of the current frame is added to the back of the index of the previous frame in the MP4 file, the added index only covers the head of the free box field, and because the free box field is an empty box, the content in the free box field has no meaning, and even if the head of the free box field is rewritten, the recording effect is not affected. In addition, because the head position of each index table is fixed and unchanged, the index written into the video file can never be damaged, thereby simplifying the memory and ensuring the recording effect.
S103: and writing the audio and video media data corresponding to the current frame into the MP4 file.
In a specific implementation process, the audio/video media data corresponding to the current frame may be written into the mdat field, so that the audio/video media data corresponding to the current frame is written into the MP4 file. After all frames have been recorded (i.e. the recording is finished), the corresponding MP4 file is obtained.
In the embodiment of the present invention, as shown in fig. 5, in step S102: before writing the index of the current frame to the end position of the index table related to the target moov field in the MP4 file, the method further includes:
s201: setting a header space of the MP4 file, wherein the header space is used for storing a written preset moov field;
s202: after recording audio and video media data with a first preset frame number according to the preset moov field, determining the space occupied by each index table in the memory, wherein the first preset frame number is an integer greater than 1;
s203: dividing the head space according to the space size of the memory occupied by each index table, and determining the position and size of each index table in the head space;
s204: and updating the preset moov field to the target moov field.
In the specific implementation process, the specific implementation process from step S201 to step S204 is as follows:
first, setting the size of the header space of the MP4 file may be by calling the relevant application interface to set the size "headersize" of the header space of the MP4 file. The written preset moov field can be stored through the head space, and accordingly, the position and the size of each index table are conveniently set. In order to avoid the oversize of the whole file, the set size of the head space can be set as small as possible, and in addition, the preset moov field can be a preset moov field which can be in a structure like that of fig. 1.
And then, after recording audio and video media data with a first preset frame number according to the preset moov field, determining the space size occupied by each index table, wherein the first preset frame number is a preset frame number, and is specifically an integer greater than 1. For example, if the configured headersize is less than 100k, the first preset frame number may be 100 frames, and if the configured headersize is greater than or equal to 100k, the first preset frame number may be 1000 frames. As a specific example, after recording 1000 frames of video according to the structure shown in fig. 1, counting the space size occupied by each of the stts, stss, stsz, and stco index tables, and determining the space size occupied by each index table; then, according to the space size of the memory occupied by each index table, the head space is divided, and the position and the size of each index table in the head space are determined. That is to say, after the recorded audio and video media data with the first preset frame number, the sizes of the spaces occupied by the index tables in the memory are counted, and then the positions and sizes of the index tables in the head space are adjusted, so as to update the preset moov fields, which may be updating the preset moov fields to the target moov fields shown in fig. 3, and further, to implement reasonable allocation of the head space, and improve the utilization rate of the head space in the MP4 file.
In the embodiment of the present invention, as shown in fig. 6, step S203: according to the size of the space occupied by each index table in the memory, the head space is divided, and the position and the size of each index table in the head space are determined, including:
s301: determining the ratio of the space size occupied by each index table to the total space size occupied by the at least four index tables according to the space size occupied by each index table to the memory;
s302: and dividing the head space according to the ratio, and determining the maximum length of each index table and the corresponding free box field and the position of the corresponding table header in the MP4 file.
In the specific implementation process, the specific implementation process from step S301 to step S302 is as follows:
firstly, determining the ratio of the space size of the memory occupied by each index table to the total space size of the memory occupied by at least four index tables according to the space size of the memory occupied by each index table, for example, the space sizes of the memories occupied by the four index tables of stts, stss, stsz and stco are S1, S2, S3 and S4 in sequence, the total space size of the memories occupied by the four index tables is S, and the ratio is S1: s2: s3: s4 is 1: 2: 3: 4. of course, the ratio of the space size of the memory occupied by each corresponding index table to the total space size of the memory occupied by at least four index tables may be determined according to the actual recording condition, and is not limited herein.
After determining the ratio of the space size occupied by each index table to the total space size occupied by at least four index tables, dividing the header space according to the ratio, and determining the maximum length of each index table and the corresponding free box field and the position of the corresponding header in the MP4 file, where the header of each index table may be as shown in offset1, offset2, offset3 and offset4 in fig. 3. The head space may be divided into a plurality of parts according to an equal proportion method, and the space size of the corresponding part may be allocated according to the ratio of each index table. Still in the above ratio S1: s2: s3: s4 is 1: 2: 3: for example, the head space may be divided into 10 parts in equal proportion, the first part may be allocated to the stts index table and the following free box field, the second part and the third part may be allocated to the sts index table and the following free box field, the fourth part to the sixth part may be allocated to the stsz index table and the following free box field, and the seventh part to the tenth part may be allocated to the stco index table and the following free box field, so that each index table is allocated with a more appropriate space size, and the maximum length of each index table and the corresponding free box field is guaranteed to be a more appropriate space size. After the header space is divided, the maximum length of each index table and the corresponding free box field is allocated, and then the position of the corresponding table header of each index table in the MP4 file can be determined. During the subsequent recording process, the position of the corresponding table header of each index table in the MP4 file can be kept constant. Therefore, the reasonable distribution of the space size and the header position of the index table is realized, the situation that the distributed space is written to other index tables after being written is avoided, and the recording quality is ensured.
In the specific implementation process, as the headersize is not abundant, the spatial size and the position of each index table and the corresponding free box field are adaptively adjusted by performing statistical analysis on the audio/video media data with the first preset frame number, so that the spatial size and the head position of the index table are reasonably distributed. If the headersize is less than 100k, the space size and the position of each index table can be adjusted in a self-adaptive manner at the 100 th frame; if the headersize is not less than 100k, each index table can be adaptively adjusted at the 1000 th frame. The adaptive adjustment of each index table at the time of the several frames can be determined by the set size of the head space.
In the embodiment of the invention, in the steps: in the process of recording the audio and video media data with the first preset frame number according to the preset moov field, the method further comprises:
and storing the index of each frame in the audio and video media data with the first preset frame number in the memory, and writing the indexes of all frames corresponding to the audio and video media data with the first preset frame number into the MP4 file.
In a specific implementation process, in the process of recording the audio and video media data with the first preset frame number according to the preset moov field, the index of each frame in the audio and video media data with the first preset frame number may be stored in the memory, the audio and video media data with the first preset frame number may be the frame number after the MP4 file starts to be recorded, the audio and video media data with the first preset frame number may be recorded according to the moov field shown in fig. 1, and the index of each frame in the audio and video media data with the first preset frame number may be stored in the memory, so that it is avoided that each index table in fig. 1 is adjacent to and the length of the previous index table is increased to break an adjacent subsequent index table. In addition, indexes of all frames corresponding to the audio and video media data with the first preset frame number can be written into an MP4 file, statistical analysis can be carried out on the condition that space size of a memory is occupied by each index table after the audio and video media data with the first preset frame number are recorded, and then adaptive adjustment is carried out on the space size and the position of each index table and a corresponding free box field in a head space, so that the subsequent recording effect is guaranteed.
In the embodiment of the present invention, in step S101: after creating an empty MP4 file, the method further comprises:
and recording and storing the corresponding frame index information content of the stco index table in the at least four index tables every second preset frame number.
In a specific implementation process, after an empty MP4 file is created, in the entire recording process, the frame index information content corresponding to the stco index table in the at least four index tables may be recorded and stored every second preset frame number, where the second preset frame number may be a preset frame number, and may be a second preset frame number set by using a samples _ per _ chunk field in the stco field of the target moov field, and a specific setting condition may be set according to an actual application requirement, for example, for the video stco index table, when the samples _ per _ chunk field is 15, a mode of recording once every 15 frames may be adopted, or for a 64-bit MP4 file, the frame index information content corresponding to the co64 index table may be recorded and stored every second preset frame number; as another example, the stco index table may be recorded once every 600ms for audio. Therefore, the occupation of the head space is avoided, and the utilization rate of the head space is improved.
In the embodiment of the present invention, in step S102: after writing the index of the current frame to the end position of the index table related to the target moov field in the MP4 file, the method further includes:
and writing fixed fields except the index tables in the target moov fields into the MP4 file.
In a specific implementation process, after the index of the current frame is written to the end position of the index table related to the target moov field in the MP4 file, the free box field behind the corresponding index table needs to be updated, and fixed fields except for the respective index tables in the target moov field need to be written into the MP4 file. That is, the target moov field includes two parts, namely a first part and a second part, the first part is an index table part, the index table part only stores the index of the current frame in the memory, the frame indexes before the current frame are all written into the MP4 file, and the second part is a fixed field part, which occupies less memory. The index of the current frame is written behind the index of the previous frame in the MP4 file, and the free box field is updated, so that the recording efficiency is improved because only the index of the current frame needs to be written each time.
In one exemplary embodiment, a method flowchart of a method for recording an MP4 file is provided as shown in fig. 7, the method comprising the steps of:
s10: starting;
s11: setting the size of a head space; specifically, the header space size headersize of the MP4 file may be set through the associated application interface, mainly for storing the moov field.
S12: after recording a plurality of frames, adaptively adjusting the space size and position of each index table and the corresponding free box field; for example, after recording 1000 frames of video, the sizes of memory spaces occupied by the stts, stss, stsz and stco index tables at the moment are counted, the header spaces are divided according to the memory space occupation ratios of the four index tables, the maximum length of each index table and the corresponding free box field and the position of the corresponding header in the MP4 file can be calculated based on an equal proportion algorithm, and the header position is kept unchanged in the subsequent recording process; in addition, a free box field is added to the rear of each index table.
S13: writing the index of the current frame into an MP4 file, and updating a free box field; specifically, the moov field is divided into two parts, the first part is an index table part which only stores the index of the current frame in the memory, and the second part is a fixed field part which occupies less memory; the index of the current frame is written behind the index of the previous frame in the MP4 file, and the free box field is updated, only the index of the current frame needs to be written each time, and the recording speed is high.
S14: writing the fixed field part in the moov field into the MP4 file;
s15: writing the audio and video media data of the current frame into an MP4 file;
s16: judging whether the recording is finished;
s17: if the recording is not finished, executing S13;
s18: and if the recording is finished, finishing.
For the specific implementation process of the above steps S10-S18, reference may be made to the description of the foregoing relevant parts, which are not described herein again.
In an exemplary embodiment, as shown in fig. 8, a flowchart of a method for recording an MP4 file by a terminal is provided, which includes the following steps:
s20: starting;
s21: creating an empty MP4 file;
s22: setting the size of the header space of the MP4 file through an interface; specifically, it is judged by the set headersize that the index table is adaptively adjusted at the time of the several frame.
S23: calculating and adjusting the index table structure in the moov field from the frame number; for example, if the headersize is less than 100k, the index table is adaptively adjusted at the 100 th frame, otherwise, the index table is adaptively adjusted at the 1000 th frame.
S24: reading in a frame of audio or video through an interface;
s25: updating the moov field, and writing the audio and video data and the updated moov field into an MP4 file; specifically, the audio and video data may be written to an mdat field in the MP4 file, and the index and other information may be stored in the moov field, at this time, the indexes of all the frames obtained by the parsing are stored in the memory, and then the moov field is written to the MP4 file.
S26: judging whether to adjust the structure of the index table; specifically, it is determined whether a specific frame is reached.
S27: if not, go to S24; specifically, if the specific frame is not reached, the audio or video continues to be read.
S28: if so, adjusting the structure of the index table, and writing the adjusted moov field and the current frame index into an MP4 file; specifically, if a specific frame, for example 1000 frames, is reached, adaptively adjusting an index table in the moov field, and writing the entire moov field including the index of the previous frame into the MP4 file according to the adjusted index table structure; therefore, the moov structure (without a free box field behind each index table) shown in fig. 1 can be adjusted to the moov structure (with a free box field behind each index table) shown in fig. 3, after the adjustment, the previous moov field is covered in the video file, and then, only the index of the current frame needs to be saved, and the index of the previous frame does not need to be saved. In this way, the space size is reasonably allocated to each index table in the MP4 file.
S29: reading in a frame of audio or video through an interface;
s30: writing the audio and video frames into an MP4 file; specifically, audio or video data is written to the mdat field in the MP4 file.
S31: additionally writing the index information of the current frame to the tail of the related index table in the MP4 file;
s32: updating a free box field behind the index table in the MP4 file;
s33: writing the fixed field part of the moov field to the MP4 file;
s34: judging whether the recording is finished;
s35: if the recording is not finished, executing S29;
s36: and if the recording is finished, finishing.
For the specific implementation process of the above steps S20-S36, reference may be made to the description of the foregoing relevant parts, which are not described herein again.
Based on the same inventive concept, as shown in fig. 9, an embodiment of the present invention further provides an apparatus for recording an MP4 file, including:
the creation unit 10: for creating an empty MP4 file;
the first processing unit 20 is configured to, when recording audio and video media data of a current frame, only store an index of the current frame in a memory, and write the index of the current frame to an end position of an index table in the MP4 file, where the index table is associated with a target moov field, the target moov field includes at least four index tables and a free box field reserved for each index table, and each index table is used to store different index information contents of a corresponding frame;
and the second processing unit 30 is configured to write the audio/video media data corresponding to the current frame into the MP4 file.
In this embodiment of the present invention, before writing the index of the current frame to the end position of the index table in the MP4 file associated with the target moov field, the recording apparatus further includes a third processing unit, where the third processing unit is configured to:
setting a header space of the MP4 file, wherein the header space is used for storing a written preset moov field;
after recording audio and video media data with a first preset frame number according to the preset moov field, determining the space occupied by each index table in the memory, wherein the first preset frame number is an integer greater than 1;
and dividing the head space according to the space size of the memory occupied by each index table, and determining the position and the size of each index table in the head space.
In an embodiment of the present invention, the third processing unit is configured to:
determining the ratio of the space size occupied by each index table to the total space size occupied by the at least four index tables according to the space size occupied by each index table to the memory;
and dividing the head space according to the ratio, and determining the maximum length of each index table and the corresponding free box field and the position of the corresponding table header in the MP4 file.
In this embodiment of the present invention, in the process of recording the audio/video media data with the first preset frame number according to the preset moov field, the third processing unit is further configured to:
and storing the index of each frame in the audio and video media data with the first preset frame number in the memory, and writing the indexes of all frames corresponding to the audio and video media data with the first preset frame number into the MP4 file.
In an embodiment of the present invention, after the creating of an empty MP4 file, the recording apparatus further includes a recording unit, where the recording unit is configured to:
and recording and storing the corresponding frame index information content of the stco index table in the at least four index tables every second preset frame number.
In this embodiment of the present invention, after writing the index of the current frame to the end position of the index table in the MP4 file associated with the target moov field, the recording apparatus further includes a fourth processing unit, where the fourth processing unit is configured to:
and writing fixed fields except the index tables in the target moov fields into the MP4 file.
Based on the same inventive concept, an embodiment of the present invention further provides a recording apparatus, which includes a processor, and the processor is configured to implement the steps of the method for recording an MP4 file as described in any one of the above when executing the computer program stored in the memory.
Based on the same inventive concept, the embodiment of the present invention further provides a readable storage medium, on which a computer program is stored, and the computer program, when executed by a processor, implements the steps of the recording method of the MP4 file as described in any one of the above.
The invention has the following beneficial effects:
the invention provides a method, a device and a readable storage medium for recording an MP4 file, firstly, an empty MP4 file is created, when audio and video media data of a current frame are recorded, only the index of the current frame is stored in a memory, and the index of the current frame is written to the tail position of an index table related to a target moov field in the MP4 file, namely, in the recording process, only the index of the current frame needs to be stored in the memory, thereby effectively reducing the memory occupation, in addition, the target moov field comprises at least four index tables used for storing different index information contents of corresponding frames, and also comprises a free box field used for storing newly added indexes of each index table, therefore, when the audio and video media data of one frame are recorded, the target moov field is updated, thereby ensuring the MP4 file to be effective after power failure, and only the recorded index of the current frame needs to be written to the tail position of the index table related to the target moov field in the MP4 file, the written data is less, and only the index of the current frame in the target moov field needs to be updated, so that the recording efficiency is improved. After the audio-video media data corresponding to the current frame is written into the MP4 file, a recorded MP4 file is obtained. Therefore, after the power is off, the MP4 file is effective, the recording efficiency is high, the memory occupation is reduced, and the recording effect of the MP4 file is improved.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to the application. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present application without departing from the spirit and scope of the application. Thus, if such modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is intended to include such modifications and variations as well.

Claims (10)

1. A method for recording MP4 files is characterized by comprising the following steps:
creating an empty MP4 file;
when recording audio and video media data of a current frame, only storing the index of the current frame in a memory, and writing the index of the current frame to the tail position of an index table related to a target moov field in the MP4 file, wherein the target moov field comprises at least four index tables and a free box field for storing newly added indexes of each index table, and each index table is respectively used for storing different index information contents of the corresponding frame;
and writing the audio and video media data corresponding to the current frame into the MP4 file.
2. The recording method as claimed in claim 1, wherein before said writing the index of the current frame to an end position of an index table in the MP4 file associated with a target moov field, the method further comprises:
setting a header space of the MP4 file, wherein the header space is used for storing a written preset moov field;
after recording audio and video media data with a first preset frame number according to the preset moov field, determining the space occupied by each index table in the memory, wherein the first preset frame number is an integer greater than 1;
dividing the head space according to the space size of the memory occupied by each index table, and determining the position and size of each index table in the head space;
and updating the preset moov field to the target moov field.
3. The recording method according to claim 2, wherein the dividing the head space according to the size of the space occupied by each index table in the memory, and determining the position and size of each index table in the head space comprises:
determining the ratio of the space size occupied by each index table to the total space size occupied by the at least four index tables according to the space size occupied by each index table to the memory;
and dividing the head space according to the ratio, and determining the maximum length of each index table and the corresponding free box field and the position of the corresponding table header in the MP4 file.
4. The recording method of claim 3, wherein in the process of recording the first preset number of frames of audio and video media data according to the preset moov field, the method further comprises:
and storing the index of each frame in the audio and video media data with the first preset frame number in the memory, and writing the indexes of all frames corresponding to the audio and video media data with the first preset frame number into the MP4 file.
5. The recording method as set forth in claim 1, wherein after the creating an empty MP4 file, the method further comprises:
and recording and storing the corresponding frame index information content of the stco index table in the at least four index tables every second preset frame number.
6. The recording method as claimed in any one of claims 1 to 5, wherein after said writing the index of the current frame to an end position of an index table in the MP4 file associated with a target moov field, the method further comprises:
and writing fixed fields except the index tables in the target moov fields into the MP4 file.
7. An apparatus for recording MP4 files, comprising:
a creation unit: for creating an empty MP4 file;
the first processing unit is configured to, when recording audio and video media data of a current frame, only store an index of the current frame in a memory, and write the index of the current frame to an end position of an index table in the MP4 file, where the index table is associated with a target moov field, the target moov field includes at least four index tables and a free box field reserved for each index table, and each index table is used to store different index information contents of a corresponding frame;
and the second processing unit is used for writing the audio and video media data corresponding to the current frame into the MP4 file.
8. The recording apparatus as claimed in claim 7, wherein, before said writing the index of the current frame to the end position of the index table in the MP4 file associated with the target moov field, the recording apparatus further comprises a third processing unit for:
setting a header space of the MP4 file, wherein the header space is used for storing a written preset moov field;
after recording audio and video media data with a first preset frame number according to the preset moov field, determining the space occupied by each index table in the memory, wherein the first preset frame number is an integer greater than 1;
and dividing the head space according to the space size of the memory occupied by each index table, and determining the position and the size of each index table in the head space.
9. A recording apparatus, characterized in that the recording apparatus comprises a processor for implementing the steps of the method for recording an MP4 file according to any one of claims 1-6 when executing a computer program stored in a memory.
10. A readable storage medium, on which a computer program is stored, the computer program, when being executed by a processor, implementing the steps of the recording method of the MP4 file according to any one of claims 1-6.
CN202111664254.XA 2021-12-31 2021-12-31 MP4 file recording method and device and readable storage medium Pending CN114374875A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111664254.XA CN114374875A (en) 2021-12-31 2021-12-31 MP4 file recording method and device and readable storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111664254.XA CN114374875A (en) 2021-12-31 2021-12-31 MP4 file recording method and device and readable storage medium

Publications (1)

Publication Number Publication Date
CN114374875A true CN114374875A (en) 2022-04-19

Family

ID=81141329

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111664254.XA Pending CN114374875A (en) 2021-12-31 2021-12-31 MP4 file recording method and device and readable storage medium

Country Status (1)

Country Link
CN (1) CN114374875A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115589480A (en) * 2022-10-09 2023-01-10 深圳市巨龙创视科技有限公司 MOov-based MP4 video restoration method and system

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
KR20130084345A (en) * 2012-01-17 2013-07-25 (주)피타소프트 Mp4 file configuring device and restoring device, mp4 file configuring method and restoring method
CN103577553A (en) * 2013-10-18 2014-02-12 汉柏科技有限公司 Data storage method
CN104063447A (en) * 2014-06-17 2014-09-24 惠州华阳通用电子有限公司 Monitoring video file restoring method
CN105847724A (en) * 2016-04-12 2016-08-10 深圳市极酷威视科技有限公司 Method and apparatus protecting video recording against outage
CN105872484A (en) * 2016-06-04 2016-08-17 武汉诚迈科技有限公司 Protection method of monitoring video abnormity
CN106055643A (en) * 2016-05-31 2016-10-26 成都索贝数码科技股份有限公司 Continuous writing method for writing interrupt of MXF files
CN108833817A (en) * 2018-06-27 2018-11-16 浙江大华技术股份有限公司 A kind of video data storage method and device
US20200382754A1 (en) * 2019-05-31 2020-12-03 Medicapture, Inc. Automatic video file repair method and system

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
KR20130084345A (en) * 2012-01-17 2013-07-25 (주)피타소프트 Mp4 file configuring device and restoring device, mp4 file configuring method and restoring method
CN103577553A (en) * 2013-10-18 2014-02-12 汉柏科技有限公司 Data storage method
CN104063447A (en) * 2014-06-17 2014-09-24 惠州华阳通用电子有限公司 Monitoring video file restoring method
CN105847724A (en) * 2016-04-12 2016-08-10 深圳市极酷威视科技有限公司 Method and apparatus protecting video recording against outage
CN106055643A (en) * 2016-05-31 2016-10-26 成都索贝数码科技股份有限公司 Continuous writing method for writing interrupt of MXF files
CN105872484A (en) * 2016-06-04 2016-08-17 武汉诚迈科技有限公司 Protection method of monitoring video abnormity
CN108833817A (en) * 2018-06-27 2018-11-16 浙江大华技术股份有限公司 A kind of video data storage method and device
US20200382754A1 (en) * 2019-05-31 2020-12-03 Medicapture, Inc. Automatic video file repair method and system

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
汤家兴;陈耀武;蒋荣欣;: "一种网络视频监控系统的专用存储方案", 计算机工程, no. 12, 15 December 2013 (2013-12-15), pages 37 - 40 *
赵丽娜;韩冬梅;薛华成;: "一种有效加密MP4文件的方法", 计算机应用与软件, no. 10, 15 October 2012 (2012-10-15), pages 28 - 30 *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115589480A (en) * 2022-10-09 2023-01-10 深圳市巨龙创视科技有限公司 MOov-based MP4 video restoration method and system

Similar Documents

Publication Publication Date Title
JP4689872B2 (en) Digital video stream decoding method and apparatus by pseudo header insertion in digital video system
US8693849B2 (en) Source optimized dynamic trickplay
CN105100876A (en) Streaming media playing method and device
US9576609B2 (en) Subtitle processing device and associated method and subtitle parsing method
US11217279B2 (en) Method and device for adjusting video playback speed
CN107371061A (en) A kind of video stream playing method, device and equipment
JP2003511918A (en) Method and apparatus for trick mode generation in a digital video system
EP2199917A1 (en) Cache control apparatus and method
JP2008160837A (en) Method and apparatus for encoding/decoding multimedia data with preview function
CN114374875A (en) MP4 file recording method and device and readable storage medium
CN111669645A (en) Video playing method and device, electronic equipment and storage medium
CN103686077A (en) Double buffering method applied to realtime audio-video data transmission of 3G wireless network
EP1765017B1 (en) Data processing methods and systems
JP2004005472A (en) Recording/storage control of contents attached with precedence
CN105578224A (en) Multimedia data acquisition method, device, smart television and set-top box
CN111966632B (en) Streaming data storage method, reading method, equipment and readable storage medium
CN104506920A (en) Method and device for playing omnimedia data information
JP4325194B2 (en) Apparatus and method for managing access to storage medium
CN108093258B (en) Code stream data decoding method, computer device and computer readable storage medium
JP4040343B2 (en) Moving image processing device
CN105847990A (en) Media file playing method and apparatus
CN102169706B (en) Method and device for storing and playing YUV file
JP3073948B2 (en) Digital video decoding apparatus and digital video decoding method
KR101124168B1 (en) Recording apparatus for multi channel pictures
US8412019B2 (en) Apparatus for and a method of storing a real time stream of digital information signals

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination