Method, device and computer readable storage medium for creating an mp4 file
By pre-setting a block space in the header of MP4 files and periodically writing metadata, the problem of missing metadata blocks during embedded device recording is solved, thus achieving high reliability and robustness of MP4 files.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-25
- Publication Date
- 2026-04-10
AI Technical Summary
Embedded devices may experience file corruption due to missing metadata blocks caused by abnormal situations such as power outages, crashes, or loose storage media during MP4 file recording, increasing inconvenience for users.
Two block spaces are pre-defined in the MP4 file header, and metadata is periodically written during the media data writing process. By modifying the block type and processing the remaining space, the reliability of the metadata is ensured.
It reduces the probability of MP4 files being corrupted due to missing metadata blocks, ensures that recorded MP4 files can be parsed normally under unexpected circumstances, and enhances file robustness.
Smart Images

Figure CN113986623B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the field of MP4 files, and in particular, to a method, an apparatus and a computer readable storage medium for creating an MP4 file. BACKGROUND
[0002] An MP4 file is composed of a plurality of boxes storing data, wherein each box has a specific type and a certain size. During the process of recording an MP4 file, media data (i.e. video and audio data) will be stored in a media data box (“mdat” box), while metadata describing various information of the media data (e.g. encoding format, location information, sampling rate, resolution, etc. of the video and audio data) will be stored in a metadata box (“moov” box). By parsing the metadata box at the player end, all the information required for playing the MP4 file can be obtained. An MP4 file missing the metadata box cannot be normally parsed and played by the player.
[0003] Generally, the operation of writing metadata into the MP4 file to generate the metadata box is performed after all the media data is written into the MP4 (i.e. after the normal recording is finished). However, embedded devices such as a driving recorder, an IP camera, etc. need to record MP4 files into the storage medium (e.g. an SD card) of the device cyclically. Abnormal situations such as power failure, video recording program crash, storage medium loosening or hot plug, etc. occurring during the recording of the device will cause the currently recording MP4 file to be forced to stop recording before the metadata box is generated, thus making the MP4 file damaged due to the missing metadata box. At this time, the damaged MP4 file needs to be transferred to the PC end to attempt to repair the MP4 file by using the MP4 repair tool of the PC end. However, this increases the inconvenience of the user using the embedded device and reduces the user experience. SUMMARY
[0004] To solve the above problems, the present disclosure provides a method, an apparatus and a computer readable storage medium for creating an MP4 file.
[0005] According to an aspect of the present disclosure, a method for creating an MP4 file is provided, comprising: presetting two block spaces for writing metadata in a header of the MP4 file; during a process of writing media data into the MP4 file, periodically performing the following operations based on a predetermined time interval: judging block types in the two block spaces to identify a to-be-written block space in the two block spaces in which block types are only a first type, wherein the first type corresponds to a block type that will be ignored when the MP4 file is parsed; writing metadata associated with the media data in the MP4 file into a to-be-written block in the to-be-written block space; and modifying a block type of the to-be-written block to a metadata block type, and modifying block types in another block space in the two block spaces to the first type.
[0006] According to an example of the present disclosure, the presetting the two block spaces for writing metadata in the header of the MP4 file comprises: generating two blocks with block types being the first type in the header of the MP4 file to obtain the preset two block spaces before media data is written into the MP4 file, wherein sizes of the two blocks are associated with a predetermined recording duration of the MP4 file.
[0007] According to an example of the present disclosure, the modifying the block types is performed after the metadata is written.
[0008] According to an example of the present disclosure, the modifying the block types in the other block space in the two block spaces to the first type is performed after the modifying the block type of the to-be-written block to the metadata block type.
[0009] According to an example of the present disclosure, the method further comprises, after all media data is written into the MP4 file, performing the following operations: judging block types in the two block spaces to identify a to-be-written block space in the two block spaces in which block types are only the first type; writing metadata associated with the all media data into a to-be-written block in the to-be-written block space; and modifying a block type of the to-be-written block to the metadata block type, and modifying block types in another block space in the two block spaces to the first type.
[0010] According to an example of the present disclosure, the method further comprises, after the metadata is written, modifying a block size field of the to-be-written block according to a size of metadata contained in the to-be-written block, and then generating a block with the first type based on a remaining space in the to-be-written block space.
[0011] According to examples of the present disclosure, before modifying the block type, the operation of modifying the block size field of the block to be written and generating a block of the first type based on the remaining space is performed.
[0012] According to another aspect of the present disclosure, there is provided an apparatus for creating an MP4 file, comprising: a preset unit configured to preset two block spaces for writing metadata in a header of the MP4 file; an update unit configured to periodically perform the following operations based on a predetermined time interval during a process of writing media data into the MP4 file: judging block types within the two block spaces to identify a block space to be written in the two block spaces in which the block type is only of a first type, wherein the first type corresponds to a block type that will be ignored when parsing the MP4 file; writing metadata associated with the media data in the MP4 file into a block to be written in the block space to be written; and modifying the block type of the block to be written to a metadata block type and modifying the block type within another block space in the two block spaces to the first type.
[0013] According to another aspect of the present disclosure, there is provided an apparatus for creating an MP4 file, comprising: a processor; and a memory storing computer program instructions, wherein the computer program instructions, when executed by the processor, cause the processor to perform the following steps: presetting two block spaces for writing metadata in a header of the MP4 file; periodically performing the following operations based on a predetermined time interval during a process of writing media data into the MP4 file: judging block types within the two block spaces to identify a block space to be written in the two block spaces in which the block type is only of a first type, wherein the first type corresponds to a block type that will be ignored when parsing the MP4 file; writing metadata associated with the media data in the MP4 file into a block to be written in the block space to be written; and modifying the block type of the block to be written to a metadata block type and modifying the block type within another block space in the two block spaces to the first type.
[0014] According to another aspect of the present disclosure, there is provided a computer readable storage medium having computer program instructions stored thereon, wherein the computer program instructions, when executed by a processor, implement the method as described in various aspects and examples of the present disclosure.
[0015] The method, apparatus and computer readable storage medium for creating an MP4 file according to the above various aspects of the present disclosure can greatly reduce the probability of the MP4 file being damaged due to missing metadata blocks caused by various unexpected conditions during the recording process of the MP4 file, ensuring that the MP4 file recorded before the unexpected condition occurs can be normally parsed by a player, and greatly enhancing the robustness of the recording and storage of the MP4 file. Attached Figure Description
[0016] The above and other objects, features, and advantages of this disclosure will become clearer through a detailed description of the embodiments of this disclosure in conjunction with the accompanying drawings. It should be understood that the drawings are used to provide a further understanding of the embodiments of this disclosure and are used together with the embodiments of this disclosure to explain this disclosure, but do not constitute a limitation thereof.
[0017] Figure 1 A schematic diagram of the MP4 file structure in the prior art is shown;
[0018] Figure 2 A flowchart of an MP4 file creation method according to an embodiment of the present disclosure is shown;
[0019] Figure 3 A schematic diagram of an MP4 file structure according to an embodiment of the present disclosure is shown;
[0020] Figure 4 A flowchart is shown of a method 400 for writing metadata at the end of MP4 file recording according to an embodiment of the present disclosure;
[0021] Figure 5 An example diagram of the MP4 file structure after the operations of modifying the block type and processing the remaining space are completed, according to an embodiment of the present disclosure;
[0022] Figure 6 A block diagram of an MP4 file creation apparatus 600 according to an embodiment of the present disclosure is shown;
[0023] Figure 7 A block diagram of an MP4 file creation apparatus 700 according to an embodiment of the present disclosure is shown. Detailed Implementation
[0024] To aid in the details provided above, the following description is provided to enable those skilled in the art to understand, make, and use the invention as described herein and expressly claimed herein. Because specific combinations of the various features will result in numerous practical embodiments in which this disclosure can be practiced, and for the purpose of providing reasonably clear and concise description, only preferred embodiments will be presented herein. However, it should be recognized that those skilled in the art can similarly practice other embodiments not expressly described herein. Thus, any assessment of the scope of this disclosure should be made with respect to the claims expressly provided herein, and the scope of this disclosure should be interpreted in accordance with its broadest reasonable interpretation and taking into account ordinary knowledge and skill in the art. Nothing in this description is intended to limit the spirit and scope of this disclosure.
[0025] The method, apparatus, and computer-readable storage medium for creating MP4 files according to embodiments of the present disclosure will now be described with reference to the accompanying drawings. In the drawings, the same reference numerals denote the same elements throughout. It should be understood that the embodiments described herein are merely illustrative and should not be construed as limiting the scope of this disclosure.
[0026] Figure 1 A schematic diagram of the MP4 file structure in the prior art is shown. For example... Figure 1 As shown, the flag block (“ftyp” box) is the first block located at the beginning of the MP4 file. It serves as an identifier for the MP4 format and contains basic information such as the encoding format and protocol used by the file. Following this are the media data block (“mdat” box) for storing media data and the metadata block (“moov” box) for storing metadata. The order of these two blocks in the MP4 file can be interchanged. However, regardless of whether the metadata block is before or after the media data block, the metadata block is typically written to the MP4 file only after all the media data has been written to the media data block. As mentioned above, this MP4 file structure and the method of writing metadata pose a significant risk of missing metadata blocks in the MP4 file due to abnormal recording interruptions.
[0027] The following will introduce an MP4 file structure and an MP4 file creation method that enhance the robustness of MP4 file recording and storage according to embodiments of the present disclosure.
[0028] Figure 2 A flowchart illustrating an MP4 file creation method according to an embodiment of this disclosure is shown. Figure 2 As shown, the MP4 file creation method 100 may include the following steps:
[0029] First, in step S201, two block spaces are preset in the header of the MP4 file for writing metadata.
[0030] In this step, the two block spaces can have the same size, and their sizes can be determined based on the amount of metadata expected to be generated. Since the amount of metadata increases with the amount of media data in the MP4 file, the size of these two block spaces can be determined based on the planned recording duration of the MP4 file. That is, the longer the planned recording duration of the MP4 file, the more media data it is expected to contain, and thus a larger block space can be reserved for storing the corresponding metadata. In a specific example, assuming the planned recording duration of each MP4 file is approximately 3 minutes, both block spaces can be set to 1 megabyte (MB), which is sufficient to accommodate all the metadata expected to be generated.
[0031] wherein, as previously described, since the flag block ("ftyp" box) should generally be located at the beginning of the MP4 file, the two block spaces pre-set at the head of the MP4 file can include: pre-setting the two block spaces after the flag block of the MP4 file. Figure 3 A schematic diagram of an MP4 file structure according to an embodiment of the present disclosure is shown. As shown, based on the MP4 file structure, in the process of creating the MP4 file, a flag block of block type "ftyp" can be written first, and then two block spaces 301 and 302 for writing metadata can be pre-set immediately after the flag block. Figure 3 As shown, based on the MP4 file structure, in the process of creating the MP4 file, a flag block of block type "ftyp" can be written first, and then two block spaces 301 and 302 for writing metadata can be pre-set immediately after the flag block.
[0032] In one example, pre-setting the two block spaces for writing metadata at the head of the MP4 file can include: before writing the media data into the MP4 file, generating two blocks of the same first type at the head of the MP4 file to obtain the pre-set two block spaces, wherein the size of the two blocks is associated with the predetermined recording duration of the MP4 file. Wherein, the first type can correspond to a block type that will be ignored when the MP4 file is parsed. In one example, the first type can be "free" type, and for a player playing the MP4 file, it will skip and ignore the "free" type block without parsing it.
[0033] Continuing to refer to Figure 3 After writing the flag block of block type "ftyp", and before writing the media data into the MP4 file to generate the media data block of block type "mdat", the pre-set block space 301 and block space 302 can be obtained by generating two blocks of the same first type after the flag block (i.e., at the head of the MP4 file). In this example, the size of the two blocks to be generated can be associated with the predetermined recording duration of the MP4 file, that is, the size of the two blocks to be generated can be determined according to the predetermined recording duration of the MP4 file, so that the size of the space they occupy is consistent with the size of the two block spaces to be pre-set.
[0034] In step S202, it is determined whether media data is being written into the MP4 file, and during the process of writing media data into the MP4 file, steps S203-S205 for writing a metadata block are periodically performed based on a predetermined time interval. The predetermined time interval is a predetermined period for writing metadata into the MP4 file. That is, according to the MP4 file creation method of the embodiments of the present disclosure, metadata is written not once after all media data is written and the recording is normally ended, but an operation of writing metadata is performed once after a certain amount of media data is written. In an example, whether the predetermined time interval has passed can be calculated by a timestamp of a video frame or an audio frame, so as to determine whether metadata should be written at this time. In a specific example, assuming that the predetermined time interval is 10s, when it is determined by the timestamp of the video frame that 10s of video frame data has been written into the MP4 file, an operation of writing metadata can be performed once.
[0035] In step S203, the block types in the two block spaces are determined to identify a to-be-written block space in the two block spaces in which the block type is only the first type. As described above, the first type corresponds to a block type that will be ignored when the MP4 file is parsed.
[0036] In this step, since the two block spaces can be preset by generating two blocks of the first type, in an initial state in which metadata has not been written in the two block spaces and the block type is not modified, the block types in the two block spaces can be both the first type, and at this time, any one of the two block spaces can be selected as the to-be-written block space to write metadata therein. However, with subsequent metadata writing and block type modification operations, the block types in the two block spaces will change constantly, so that a block space can contain a metadata block in which metadata is written. Therefore, it is necessary to first determine a block space in which the block type is only the first type in the two block spaces, as the to-be-written block space to write metadata in this operation. Continuing to refer to Figure 3 , assuming that it is determined that the block type in the block space 301 is only the first type, and the block space 302 contains a metadata block, the block space 301 can be selected as the to-be-written block space.
[0037] In step S204, metadata associated with the media data in the MP4 file is written into a to-be-written block in the to-be-written block space. In step S205, the block type of the to-be-written block is modified to a metadata block type, and the block type in the other block space in the two block spaces is modified to the first type.
[0038] In step S204, the metadata is written into one of the two block spaces, and in step S205, the block type in the two block spaces is modified. In a preferred embodiment, the modification of the block type can be performed after the metadata is written. That is, step S205 is preferably performed after step S204. In this way, since the block type has not been modified during the writing of the metadata, even if an unexpected event such as a sudden power failure occurs at this time, causing the writing of the metadata to be interrupted, the metadata block written last time is still retained in the other block space, so that the MP4 file can still be normally parsed. Therefore, by using the two block spaces for the writing of the metadata block and using this specific execution order, the loss of the metadata block due to an unexpected event during the writing of the metadata can be prevented, so that higher reliability of the creation of the MP4 file is achieved.
[0039] In addition, step S205 actually includes two sub-steps, one of which is used to modify the block type of the to-be-written block in which the metadata is currently written from the first type to the metadata block type, and the other of which is used to modify the type of the metadata block in the other block space to the first type. In a further preferred embodiment, in step S205, the operation of modifying the block type in the other block space of the two block spaces to the first type can be performed after the operation of modifying the block type of the to-be-written block to the metadata block type. That is, the operation of modifying the block type of the to-be-written block to the metadata block type is first performed, and at this time, if an unexpected event such as a sudden power failure occurs, two metadata blocks will exist in the MP4 file at the same time, and the player will extract the metadata information according to the metadata block parsed first when parsing the MP4 file, so that the MP4 file can still be normally parsed. Therefore, this execution order can prevent the loss of the metadata block due to an unexpected event during the modification of the block type, so that higher reliability of the creation of the MP4 file is achieved.
[0040] After step S205 is performed, the new metadata block in which the metadata is stored is written in the MP4 file, and the metadata block written last time is modified to the first type of block. Subsequently, new media data can be continuously written into the MP4 file, and after a predetermined time interval, steps S203-S205 for writing the metadata block are performed again. Finally, after all the media data is written into the MP4 file (corresponding to "No" in step S202), the method 400 for writing the metadata at the end of the recording of the MP4 file can be performed.
[0041] Figure 4 A flowchart of the method 400 for writing the metadata at the end of the recording of the MP4 file according to an embodiment of the present disclosure is shown.
[0042] Specifically, in the method 400, after all the media data is written into the MP4 file, the following operations are performed: in step S401, judging the block types in the two block spaces to identify the to-be-written block space in the two block spaces whose block type is only the first type. In step S402, writing the metadata associated with all the media data into the to-be-written block in the to-be-written block space. In step S403, modifying the block type of the to-be-written block to the metadata block type, and modifying the block types in the other block space in the two block spaces to the first type. That is, the operation of writing the metadata into the MP4 file at the end of the MP4 file recording can be implemented by using the same processing as steps S203-S205 described above. Subsequently, the entire process of creating the MP4 file is completed to generate the final MP4 file.
[0043] Wherein, after the metadata is written into the to-be-written block in the to-be-written block space (i.e., after steps S204 and S402 described above), the MP4 file creation method according to the embodiments of the present disclosure can further include: modifying the block size field of the to-be-written block according to the size of the metadata contained in the to-be-written block, and then generating a block with the first type based on the remaining space in the to-be-written block space. Specifically, each time after the metadata is written into the to-be-written block space, the written metadata can not fill the entire to-be-written block space, so there is usually a remaining space in the to-be-written block space that is not used. If this remaining space is not processed, errors are likely to occur when the MP4 file is parsed. Therefore, the present disclosure further generates a block with the first type based on the remaining space in the to-be-written block space that is not occupied by the to-be-written block after the metadata is written into the to-be-written block. Wherein, generating a block with the first type based on the remaining space in the to-be-written block space can include: setting the block size field of the first type of block to be generated according to the size of the remaining space in the to-be-written block space. As described above, since the first type of block will be skipped and ignored by the player, the player will ignore the size of this remaining space when parsing, so that this remaining space will not affect the parsing of the MP4 file, ensuring that the final generated MP4 file can be normally parsed.
[0044] In a preferred embodiment, the operation of modifying the block size field of the to-be-written block and generating the block of the first type based on the remaining space can be performed before modifying the block type. That is, the operation of processing the remaining space described above can be performed before modifying the block type. In this way, if an unexpected condition such as a sudden power failure occurs during the processing of the remaining space, the metadata block written last time is still stored in the MP4 file, so that the MP4 file can still be normally parsed. Therefore, this execution order can prevent the loss of metadata blocks caused by unexpected conditions occurring during the processing of the remaining space, thereby achieving higher reliability of MP4 file creation.
[0045] Figure 5 An example diagram of the MP4 file structure after the operation of modifying the block type and processing the remaining space is completed is shown according to an embodiment of the present disclosure. As shown in Figure 5 , it is assumed that the structure of the MP4 file before writing metadata this time is the structure shown in the lower part of Figure 5 , it can be first determined that the block type in the block space 301 is only the first type, and the block space 301 is taken as the to-be-written block space this time. As shown in Figure 5 , in the case where the to-be-written block space 301 includes two blocks, the block located at the front (i.e., the block 501) can be selected as the to-be-written block to which metadata is written this time. Subsequently, the metadata can be written into the to-be-written block 501 to obtain a to-be-written block 502 after the metadata is written. As can be seen, since the size of the block 502 is changed compared with the block 501, the remaining space 503 after the block 502 needs to be processed again. Accordingly, the block of the first type can be generated based on the remaining space 503 first, and then the operation of modifying the block type of the block 502 to the metadata block type and the operation of modifying the block type in the other block space 302 to the first type are performed. As shown in Figure 5 , finally, through a series of related operations, the structure of the MP4 file changes from the structure shown in the lower part of Figure 5 to the structure shown in the upper part of Figure 5 . When metadata is written next time, the block space 302 is selected as the to-be-written block space, and the structure of the MP4 file can change from the structure shown in the upper part of Figure 5 to the structure shown in the lower part of Figure 5 . Therefore, the structure of the MP4 file according to the embodiment of the present disclosure can change between the structures shown in the upper and lower parts of Figure 5 .
[0046] According to the MP4 file creation method of the various aspects of the present disclosure, the probability of the MP4 file being damaged due to the missing metadata block caused by the occurrence of various unexpected conditions in the MP4 file recording process can be greatly reduced, and it is ensured that the MP4 file recorded before the occurrence of the unexpected condition can be normally parsed by the player, and the robustness of the MP4 file recording and storage is greatly enhanced. In addition, in one aspect, the present disclosure uses two fixed-size block spaces preset in the header of the MP4 file to periodically write metadata, which can avoid the written metadata from covering the media data in the MP4 file, thereby preventing the influence of the periodic writing of the metadata on the media data. In another method, the present disclosure processes the remaining space in the block space to be written, so that the remaining space will not affect the parsing of the MP4 file, and it is ensured that the finally generated MP4 file can be normally parsed. In yet another aspect, the present disclosure uses a specific order to perform the various operations of writing metadata, modifying block types, and processing the remaining space, which can prevent the metadata block from being missing due to the unexpected conditions occurring in the process of various operations, thereby achieving higher reliability of MP4 file creation.
[0047] Hereinafter, an MP4 file creation device according to an embodiment of the present disclosure will be described with reference to Figure 6 Figure 6 A block diagram of an MP4 file creation device 600 according to an embodiment of the present disclosure is shown. As shown in Figure 6 The MP4 file creation device 600 includes a preset unit 610 and an update unit 620. In addition to the above units, the MP4 file creation device 600 can also include other components, however, since these components are irrelevant to the content of the embodiments of the present disclosure, their illustrations and descriptions are omitted here.
[0048] First, the preset unit 610 is configured to preset two block spaces for writing metadata in the header of the MP4 file.
[0049] The two block spaces can have the same size, and the size of the two block spaces can be determined based on the amount of metadata finally expected to be generated. Since the amount of metadata will increase accordingly with the increase of the media data in the MP4 file, the preset unit 610 can determine the size of the two block spaces according to the predetermined recording duration of the MP4 file. That is, the longer the predetermined recording duration of the MP4 file, the more media data it finally expects to contain, and then a larger block space for storing the corresponding metadata can be reserved. In one specific example, assuming that the predetermined recording duration of each MP4 file is about 3 minutes, the preset unit 610 can set the size of the two block spaces to 1 megabyte (MB) each, and such size is already sufficient to accommodate all the metadata finally expected to be generated.
[0050] In this case, as previously described, since the flag block ("ftyp" box) should generally be located at the start of the MP4 file, the two block spaces preset at the head of the MP4 file can include: presetting the two block spaces after the flag block of the MP4 file. Figure 3 A schematic diagram of an MP4 file structure according to an embodiment of the present disclosure is shown. As shown, based on the MP4 file structure, in the process of creating an MP4 file, a flag block of block type "ftyp" can be written first, and then two block spaces 301 and 302 for writing metadata can be preset immediately after the flag block. Figure 3
[0051] In one example, the presetting unit 610, configured to preset two block spaces for writing metadata at the head of the MP4 file, can include: before writing the media data into the MP4 file, the presetting unit 610 is configured to generate two blocks of the first type at the head of the MP4 file to obtain the preset two block spaces, wherein the size of the two blocks is associated with the predetermined recording duration of the MP4 file. In this case, the first type can correspond to a block type that will be ignored when the MP4 file is parsed. In one example, the first type can be the "free" type, and for a player playing the MP4 file, it will skip and ignore the "free" type block without parsing it.
[0052] Continuing to refer to Figure 3 After the flag block of block type "ftyp" is written, and before the media data is written into the MP4 file to generate the media data block of block type "mdat", the two block spaces 301 and 302 can be obtained by generating two blocks of the first type after the flag block (i.e., at the head of the MP4 file). In this example, the presetting unit 610 can associate the size of the two blocks to be generated with the predetermined recording duration of the MP4 file, that is, the presetting unit 610 can determine the size of the two blocks to be generated according to the predetermined recording duration of the MP4 file, so that the size of the space they occupy is consistent with the size of the two block spaces to be preset.
[0053] The updating unit 620 is configured to determine whether media data is being written into the MP4 file, and periodically perform steps S203-S205 for writing the metadata block based on a predetermined time interval during the process of writing the media data into the MP4 file. The predetermined time interval is a predetermined period for writing the metadata into the MP4 file. That is, the present disclosure does not write the metadata once after writing all the media data and normal recording ends, but performs the operation of writing the metadata once after writing a certain amount of media data. In one example, the updating unit 620 can determine whether the predetermined time interval has passed by calculating the time stamp of the video frame or the audio frame, so as to determine whether the metadata should be written at this time. In a specific example, assuming that the predetermined time interval is 10s, when it is determined by the time stamp of the video frame that 10s of video frame data has been written into the MP4 file, the updating unit 620 can perform the operation of writing the metadata once.
[0054] The updating unit 620 is further configured to determine the block types in the two block spaces, so as to identify the to-be-written block space in the two block spaces in which the block type is only the first type. As described above, the first type corresponds to the block type that will be ignored when the MP4 file is parsed.
[0055] Since the two block spaces can be preset by generating two blocks of the first type, in the initial state in which the metadata has not been written in the two block spaces and the block type has not been modified, the block types in the two block spaces can be both the first type, and at this time, any one of the two block spaces can be selected as the to-be-written block space to write the metadata therein. However, with subsequent metadata writing and block type modification operations, the block types in the two block spaces will constantly change, so that a block space can contain a metadata block in which the metadata is written. Therefore, the updating unit 620 needs to first determine the block space in which the block type is only the first type in the two block spaces, so as to use it as the to-be-written block space to write the metadata in this operation. Continuing to refer to Figure 3 , assuming that in the operation of writing the metadata in this time, the updating unit 620 determines that the block type in the block space 301 is only the first type, and the block space 302 contains a metadata block, the block space 301 can be used as the to-be-written block space.
[0056] The updating unit 620 is further configured to write the metadata associated with the media data in the MP4 file into the to-be-written block in the to-be-written block space. In addition, the updating unit 620 is further configured to modify the block type of the to-be-written block to the metadata block type, and modify the block type in the other block space in the two block spaces to the first type.
[0057] In a preferred embodiment, the updating unit 620 can perform the operation of modifying the type of the block after writing the metadata. In this way, since the type of the block has not been modified in the process of writing the metadata, even if an unexpected condition such as a sudden power failure occurs at this time, causing the writing of the metadata to be interrupted, the last written metadata block is still retained in the other block space, so that the MP4 file can still be normally parsed. Therefore, by using two block spaces to write the metadata block and using this specific execution order, the loss of the metadata block due to an unexpected condition occurring in the process of writing the metadata can be prevented, so that higher reliability of the creation of the MP4 file is achieved.
[0058] In addition, in a further preferred embodiment, the operation of modifying the type of the block in the other block space to the first type can be performed after the operation of modifying the type of the to-be-written block to the metadata block type. That is, the updating unit 620 can first perform the operation of modifying the type of the to-be-written block to the metadata block type, and at this time, if an unexpected condition such as a sudden power failure occurs, there will be two metadata blocks in the MP4 file, and the player will extract the metadata information according to the first parsed metadata block when parsing the MP4 file, so that the MP4 file can still be normally parsed. Therefore, this execution order can prevent the loss of the metadata block due to an unexpected condition occurring in the process of modifying the type of the block, so that higher reliability of the creation of the MP4 file is achieved.
[0059] After performing the above operations, the updating unit 620 has written a new metadata block storing metadata in the MP4 file, and modified the last written metadata block to a block of the first type. Subsequently, the updating unit 620 can continue to write new media data into the MP4 file, and after a predetermined time interval, perform the above operation of writing the metadata block again. Finally, after all the media data is written into the MP4 file, the updating unit 620 can perform the operation of writing the metadata at the end of the recording of the MP4 file.
[0060] Specifically, after all the media data is written into the MP4 file, the updating unit 620 is configured to perform the following operations: judging the block types in the two block spaces to identify a to-be-written block space in the two block spaces whose block type is only the first type; writing the metadata associated with all the media data into the to-be-written block in the to-be-written block space; modifying the block type of the to-be-written block to the metadata block type, and modifying the block types in the other block space in the two block spaces to the first type. That is, the operation of writing the metadata into the MP4 file at the end of the recording of the MP4 file can be implemented by using the same processing as the processing in the process of writing the media data described above. Subsequently, the updating unit 620 completes the entire process of the creation of the MP4 file to generate the final MP4 file.
[0061] In the above description, the updating unit 620 can further perform the following processing after writing the metadata into the to-be-written block in the to-be-written block space: modifying the block size field of the to-be-written block according to the size of the metadata contained in the to-be-written block, and then generating a block of the first type based on the remaining space in the to-be-written block space. Specifically, each time after writing the metadata into the to-be-written block space, the written metadata can not fill the entire to-be-written block space, and thus there is usually a remaining space in the to-be-written block space that is not used. If this remaining space is not processed, an error is likely to occur when the MP4 file is parsed. Therefore, the present disclosure further generates a block of the first type based on the remaining space in the to-be-written block space that is not occupied by the to-be-written block after writing the metadata into the to-be-written block. The generation of the block of the first type based on the remaining space in the to-be-written block space can include setting the block size field of the generated block of the first type according to the size of the remaining space in the to-be-written block space. As described above, since the block of the first type is skipped and ignored by the player, the player will ignore the size of this remaining space when parsing, so that this remaining space will not affect the parsing of the MP4 file, and thus ensures that the finally generated MP4 file can be normally parsed.
[0062] In a preferred embodiment, the updating unit 620 can perform the operation of modifying the block size field of the to-be-written block and generating a block of the first type based on the remaining space before modifying the block type. That is, the updating unit 620 can perform the above processing of the remaining space before modifying the block type. In this way, if an unexpected situation such as a sudden power failure occurs during the processing of the remaining space, the metadata block written last time is still stored in the MP4 file, so that the MP4 file can still be normally parsed. Therefore, this execution order can prevent the loss of the metadata block caused by the unexpected situation during the processing of the remaining space, thereby achieving higher reliability of the creation of the MP4 file.
[0063] Figure 5 An example diagram of the MP4 file structure after the operation of modifying the block type and processing the remaining space is completed is shown according to an embodiment of the present disclosure. As shown, assuming that the structure of the MP4 file before the current metadata writing is Figure 5 Figure 5 , the update unit 620 can first determine that the block type in the block space 301 is only the first type, and take the block space 301 as the current to-be-written block space. As shown, Figure 5 , in the case where the to-be-written block space 301 includes two blocks, the update unit 620 can select the block located at the front (i.e., the block 501) as the to-be-written block to which the metadata is to be written. Subsequently, the update unit 620 can write the metadata into the to-be-written block 501 to obtain a to-be-written block 502 after the metadata is written. As can be seen, since the size of the block 502 is changed compared with the block 501, it is necessary to re-process the remaining space 503 after the block 502. Accordingly, the update unit 620 can first generate a block of the first type based on the remaining space 503, and then perform the operation of modifying the block type of the block 502 to the metadata block type and the operation of modifying the block type in the other block space 302 to the first type. As shown, Figure 5 , finally, through a series of related operations, the structure of the MP4 file is changed from Figure 5 Figure 5 to the structure shown in the upper part of Figure 5 . When writing metadata next time, the update unit 620 can select the block space 302 as the to-be-written block space, and through a series of related operations, the structure of the MP4 file is changed from the structure shown in the upper part of Figure 5 to the structure shown in the lower part of Figure 7 . Therefore, the structure of the MP4 file according to the embodiment of the present disclosure can change between the structures shown in the upper and lower parts of
[0064] According to the MP4 file creation apparatus of the present disclosure, the probability of MP4 files being corrupted due to missing metadata blocks caused by various unexpected situations during MP4 file recording can be greatly reduced, ensuring that MP4 files recorded before the occurrence of unexpected situations can be parsed normally by the player, thus significantly enhancing the robustness of MP4 file recording and storage. Furthermore, on the one hand, the MP4 file creation apparatus of the present disclosure uses two fixed-size block spaces preset in the MP4 file header to periodically write metadata, which can prevent the written metadata from overwriting the media data in the MP4 file, thereby preventing the periodic writing of metadata from affecting the media data. On the other hand, the MP4 file creation apparatus of the present disclosure processes the remaining space in the block space to be written, ensuring that this remaining space will not affect the parsing of the MP4 file, guaranteeing that the finally generated MP4 file can be parsed normally. Moreover, the MP4 file creation apparatus of the present disclosure uses a specific order to perform the various operations of writing metadata, modifying block types, and processing remaining space, which can prevent the loss of metadata blocks caused by unexpected situations during various operations, thereby achieving higher reliability in MP4 file creation.
[0065] Below, refer to Figure 7 This invention describes an MP4 file creation apparatus according to embodiments of the present disclosure. Figure 7 A block diagram of an MP4 file creation apparatus 700 according to an embodiment of the present disclosure is shown. Figure 7 As shown, the MP4 file creation device 700 can be a computer or a server.
[0066] like Figure 7 As shown, the MP4 file creation apparatus 700 includes one or more processors 710 and a memory 720. In addition, the MP4 file creation apparatus 700 may also include input devices, output devices (not shown), etc., and these components can be interconnected via a bus system and / or other forms of connection mechanisms. It should be noted that... The components and structure of the MP4 file creation device 700 shown are merely exemplary and not limiting. The MP4 file creation device 700 may also have other components and structures as needed.
[0067] The processor 710 can be a central processing unit (CPU) or other form of processing unit that has data processing and / or instruction executing capabilities, and can utilize computer program instructions stored in the memory 720 to perform desired functions, which can include: presetting two block spaces in a header of an MP4 file for writing metadata; during a process of writing media data into the MP4 file, periodically performing the following operations based on a predetermined time interval: judging block types in the two block spaces to identify a to-be-written block space in the two block spaces in which a block type is only a first type, where the first type corresponds to a block type that will be ignored when the MP4 file is parsed; writing metadata associated with the media data in the MP4 file into a to-be-written block in the to-be-written block space; and modifying the block type of the to-be-written block to a metadata block type, and modifying a block type in another block space of the two block spaces to the first type.
[0068] The memory 720 can include one or more computer program products that can include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. One or more computer program instructions can be stored on the computer-readable storage media, which can be run by the processor 710 to implement the functions of the sub-space connection relationship determination apparatus of the embodiments of the present disclosure described above and / or other desired functions, and / or to perform the visual mileage calculation method according to the embodiments of the present disclosure. Various application programs and various data can also be stored in the computer-readable storage media.
[0069] Hereinafter, a computer-readable storage medium according to an embodiment of the present disclosure is described, which stores computer program instructions, where the computer program instructions are executed by a processor to implement the MP4 file creation method described above.
[0070] Of course, the specific embodiments described above are only exemplary and not limiting, and those skilled in the art can combine and combine some steps and apparatuses from the above separately described various embodiments according to the concept of the present disclosure to achieve the effect of the present disclosure, and such combined and combined embodiments are also included in the present disclosure, and such combination and combination is not described here.
[0071] Note that the advantages, advantages, effects, etc. mentioned in the present disclosure are only examples and not limitations, and these advantages, advantages, effects, etc. cannot be considered as necessary for each embodiment of the present disclosure. In addition, the specific details of the above invention are only for the purpose of example and for the purpose of understanding, and not for the purpose of limitation, and the above details do not limit the present disclosure to the above specific details.
[0072] The block diagrams of devices, apparatuses, devices, and systems disclosed herein are merely illustrative examples and are not intended to require or imply that they must be connected, arranged, or configured in the manner shown in the block diagrams. As those skilled in the art will recognize, these devices, apparatuses, devices, and systems can be connected, arranged, and configured in any manner. Words such as “comprising,” “including,” “having,” etc., are open-ended terms meaning “including but not limited to,” and are used interchangeably with them. The terms “or” and “and” as used herein refer to the terms “and / or,” and are used interchangeably with them unless the context clearly indicates otherwise. The term “such as” as used herein refers to the phrase “such as but not limited to,” and is used interchangeably with it.
[0073] The flowcharts and method descriptions in this disclosure are merely illustrative examples and are not intended to require or imply that the steps of the various embodiments must be performed in the given order. As those skilled in the art will recognize, the steps in the above embodiments can be performed in any order. Words such as "then," "next," etc., are not intended to limit the order of the steps; these words are only used to guide the reader through the description of these methods. Furthermore, any reference to a singular element, such as the use of the articles "a," "one," or "the," is not to be construed as limiting that element to the singular.
[0074] Furthermore, the steps and apparatus in the various embodiments herein are not limited to any one embodiment. In fact, new embodiments can be conceived by combining relevant steps and apparatus in the various embodiments herein based on the concepts of this disclosure, and these new embodiments are also included within the scope of this disclosure.
[0075] Each operation described above can be performed by any suitable means capable of performing the corresponding function. Such means may include various hardware and / or software components and / or modules, including but not limited to circuits, application-specific integrated circuits (ASICs), or processors.
[0076] The various exemplified logic blocks, modules, and circuits described herein can be implemented or performed using a general-purpose processor, digital signal processor (DSP), ASIC, field-programmable gate array (FPGA) or other programmable logic device (PLD), discrete gate or transistor logic, discrete hardware components, or any combination thereof designed to perform the functions described herein. The general-purpose processor may be a microprocessor, but alternatively, it may be any commercially available processor, controller, microcontroller, or state machine. The processor may also be implemented as a combination of computing devices, such as a combination of a DSP and a microprocessor, multiple microprocessors, one or more microprocessors cooperating with a DSP core, or any other such configuration.
[0077] The steps of a method or algorithm described in connection with the present disclosure can be embodied directly in hardware, in a software module executed by a processor, or in a combination of the two. A software module can reside in any form of tangible storage medium. Some examples of storage media that can be used include random access memory (RAM), read only memory (ROM), flash memory, EPROM memory, EEPROM memory, registers, a hard disk, a removable disk, a CD-ROM and so forth. A storage medium can be coupled to a processor such that the processor can read information from, and write information to, the storage medium. In the alternative, the storage medium can be integral to the processor. The software module can comprise single instruction or many instructions, and can be distributed over several different code segments, among different programs, and across multiple storage media.
[0078] The methods of the present invention include one or more acts for accomplishing the methods described. The methods and / or acts can be interchanged between methods, unless specifically stated to the contrary.
[0079] The functions described can be implemented in hardware, software, firmware or any combination thereof. If implemented in software, the functions can be stored as one or more instructions on a tangible computer-readable medium. A storage media can be any available media that can be accessed by a computer. By way of example, and not limitation, such computer-readable media can comprise RAM, ROM, EEPROM, CD-ROM or other optical disk storage, magnetic disk storage or other magnetic storage devices, or any other tangible medium that can be used to carry or store desired program code in the form of instructions or data structures and that can be accessed by a computer. Disk herein, refers to compact discs, laser discs, optical discs, digital versatile discs (DVD), floppy disks and Blu-ray discs, as examples.
[0080] Thus, a computer program product can perform the operations presented herein. For example, such a computer program product can be a computer readable tangible medium having instructions stored thereon that, when executed by one or more processors, perform the operations described herein. The computer program product can also include packaging materials.
[0081] Software or instructions can also be transmitted over a transmission medium. For example, software can be transmitted from a website, server, or other remote source using a transmission medium such as a coaxial cable, fiber optic cable, twisted pair, digital subscriber line (DSL), or wireless technologies such as infrared, radio, and microwave. Thus, a transmission medium can transmit information for execution by a machine.
[0082] Moreover, the modules and / or other appropriate means for performing the methods and techniques described herein can be downloaded and / or otherwise obtained by a user terminal and / or base station as applicable, such as from the server. For example, such a device can be coupled to the server to facilitate the transfer of means for performing the methods described herein. Alternatively, various methods described herein can be provided via a storage means (e.g., RAM, ROM, a physical storage medium such as a CD or floppy disk, etc.), such that a user terminal and / or base station can obtain the various methods upon coupling or providing the storage means to the device.
[0083] Other examples and implementations are within the scope and spirit of the disclosure and appended claims. For example, due to the nature of software, many modifications such as those relating to software
[0084] Various changes, modifications and alterations to the techniques described herein can be made without departing from the teachings of the disclosure as defined by the appended claims. Moreover, the scope of the claims of this disclosure is not limited to the specific aspects described above. Rather, the aspects of the disclosure are meant to cover all alternatives, modifications, and equivalents falling within the scope of the claims. Accordingly, the disclosure is not limited to that precisely as shown and described.
[0085] The above description of the aspects of the disclosure has been presented for the purpose of illustration and description. It is not intended to be exhaustive or to limit the aspects of the disclosure to the precise forms disclosed. Many modifications and variations are possible in light of the above teachings. It is intended that the scope of the aspects of the disclosure be limited not by this detailed description, but rather by the claims of this disclosure. As noted above, various aspects of the disclosure have been described in relation to particular examples, which have been seen, in the interest of clarity of exposition, as separate embodiments. It will of course be appreciated that where any feature is, explicitly or implicitly, included in any claim, it has been included in substitution, either alone or in
[0086] The foregoing description has been presented for the purposes of illustration and description. Furthermore, the description is not intended to limit the embodiments of the disclosure to the form disclosed herein. Although various example aspects and embodiments have been discussed above, those of ordinary skill in the art will appreciate a variety of modifications, alternatives, permutations, additions, and sub-combinations, which fall within the scope of the disclosed embodiments.
Claims
1. A method for creating an MP4 file, comprising: generating two blocks with a same block type of a first type at a head of the MP4 file to obtain two block spaces preset for writing metadata before media data is written into the MP4 file, wherein a size of the two blocks is associated with a predetermined recording duration of the MP4 file; periodically performing the following operations based on a predetermined time interval during a process of writing media data into the MP4 file: judging block types in the two block spaces to identify a to-be-written block space in the two block spaces in which block types are only the first type, wherein the first type corresponds to a block type that will be ignored when the MP4 file is parsed; calculating whether the predetermined time interval has elapsed by a timestamp of a video frame or an audio frame to determine whether metadata should be written at this time, and writing metadata associated with media data in the MP4 file into a to-be-written block in the to-be-written block space in a case where the predetermined time interval has elapsed; and modifying a block type of the to-be-written block to a metadata block type and modifying block types in another block space of the two block spaces to the first type, wherein a block size field of the to-be-written block is modified according to a size of metadata contained in the to-be-written block after the metadata is written, and then a block with the first type is generated based on a remaining space in the to-be-written block space, wherein generating the block with the first type based on the remaining space in the to-be-written block space comprises setting a block size field of the generated block with the first type according to the size of the remaining space in the to-be-written block space.
2. The method of claim 1, wherein, The modifying operation of the block type is performed after the metadata is written.
3. The method of claim 2, wherein, The operation of modifying the block types in the other block space of the two block spaces to the first type is performed after the operation of modifying the block type of the to-be-written block to the metadata block type.
4. The method of claim 2, wherein, The method further comprises, after all media data is written into the MP4 file, performing the following operations: judging block types in the two block spaces to identify a to-be-written block space in the two block spaces in which block types are only the first type; writing metadata associated with the all media data into a to-be-written block in the to-be-written block space; and modifying a block type of the to-be-written block to the metadata block type and modifying block types in another block space of the two block spaces to the first type. The operations of modifying the block size field of the to-be-written block and generating the block with the first type based on the remaining space are performed before the block types are modified.
5. The method of claim 1, wherein, 6.An apparatus for creating an MP4 file, comprising: a preset unit configured to generate two blocks with a same block type of a first type at a head of the MP4 file to obtain two block spaces preset for writing metadata before media data is written into the MP4 file, wherein a size of the two blocks is associated with a predetermined recording duration of the MP4 file; an updating unit configured to periodically perform the following operations based on a predetermined time interval during writing of media data into the MP4 file: determining the block types within the two block spaces to identify a to-be-written block space in the two block spaces in which the block type is only the first type, wherein the first type corresponds to a block type that will be ignored when the MP4 file is parsed; calculating whether the predetermined time interval has elapsed by means of a timestamp of a video frame or an audio frame to determine whether metadata should be written at this time, and in the case where the predetermined time interval has elapsed, writing metadata associated with the media data in the MP4 file into a to-be-written block in the to-be-written block space; and modifying the block type of the to-be-written block to a metadata block type and modifying the block type within the other block space of the two block spaces to the first type, wherein, after the metadata is written, a block size field of the to-be-written block is modified according to the size of the metadata contained in the to-be-written block, and subsequently, a block of the first type is generated based on the remaining space in the to-be-written block space, wherein generating the block of the first type based on the remaining space in the to-be-written block space comprises setting the block size field of the generated block of the first type according to the size of the remaining space in the to-be-written block space.
7. An apparatus for creating an MP4 file, comprising: a processor; and a memory storing computer program instructions, wherein the computer program instructions, when executed by the processor, cause the processor to perform the following steps: before media data is written into the MP4 file, generating two blocks of which the block types are all the first type in a header of the MP4 file to obtain two block spaces that are preset for writing metadata, wherein the sizes of the two blocks are associated with a predetermined recording duration of the MP4 file; during writing of media data into the MP4 file, periodically performing the following operations based on a predetermined time interval: determining the block types within the two block spaces to identify a to-be-written block space in the two block spaces in which the block type is only the first type, wherein the first type corresponds to a block type that will be ignored when the MP4 file is parsed; calculating whether the predetermined time interval has elapsed by means of a timestamp of a video frame or an audio frame to determine whether metadata should be written at this time, and in the case where the predetermined time interval has elapsed, writing metadata associated with the media data in the MP4 file into a to-be-written block in the to-be-written block space; and modifying the block type of the to-be-written block to a metadata block type and modifying the block type within the other block space of the two block spaces to the first type, wherein, after the metadata is written, a block size field of the to-be-written block is modified according to the size of the metadata contained in the to-be-written block, and subsequently, a block of the first type is generated based on the remaining space in the to-be-written block space, wherein generating the block of the first type based on the remaining space in the to-be-written block space comprises setting the block size field of the generated block of the first type according to the size of the remaining space in the to-be-written block space. In the method, after the metadata is written, the block size field of the to-be-written block is modified according to the size of the metadata contained in the to-be-written block, and then a block of the first type is generated based on the remaining space in the to-be-written block space, wherein generating the block of the first type based on the remaining space in the to-be-written block space comprises setting the block size field of the to-be-generated block of the first type according to the size of the remaining space in the to-be-written block space.
8. A computer readable storage medium having stored thereon computer program instructions, wherein, The computer program instructions, when executed by a processor, implement the method of any one of claims 1-5.
Citation Information
Patent Citations
Method and apparatus protecting video recording against outage
CN105847724A