Disclosure of Invention
In order to overcome at least one defect in the prior art, the invention provides a method, a device, electronic equipment and a storage medium for storing a recorded mp4 file, which adopts the following technical scheme.
In a first aspect, the present invention provides a method for storing a recorded mp4 file, comprising the steps of:
creating a folder associated with the mp4 file to be saved on an external memory when recording is started, and creating a media data object block, a stbl box partition and a recording process information file under the folder;
when recording is performed, media data is written into the media data object block, and the stbl box segment and the recording process information file are updated periodically.
In one embodiment, the method further comprises the step of:
and when the recording is finished, updating the stbl box fragments and the recording process information file.
In one embodiment, the process of creating stbl box fragments under the folder includes the steps of:
for at least one set of stbl box slices, a sub-level slice under the stbl box slices is created.
In one embodiment, the method further comprises the step of:
after recording is stopped, when a first generation instruction is received, generating a corresponding mp4 container according to the information in the folder based on the first generation instruction.
In one embodiment, the process of generating the corresponding mp4 container according to the information in the folder includes the steps of:
generating a file type container ftpy;
generating a metadata container moov according to the stbl box fragments under the folder based on the first generation instruction;
and reading the media data object block with the designated length according to the recording process information file to generate a media data container mdat.
In one embodiment, the method further comprises the step of:
and when receiving a second generation instruction, obtaining the mp4 file through the mp4 container.
In one embodiment, the length of a cycle is 1-3 minutes.
In a second aspect, the present invention provides an mp4 file storing apparatus for recording, comprising:
the creating module is used for creating a folder associated with the mp4 file to be saved on the external memory when recording is started, and creating a media data object block, a stbl box partition and a recording process information file under the folder;
and the updating module is used for writing the media data into the media data object block when recording is performed, and periodically updating the stbl box fragments and the recording process information file.
In a third aspect, the present invention provides a computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the method of any of the above embodiments when executing the program.
In a fourth aspect, the present invention provides a computer readable storage medium having stored thereon a computer program, characterized in that the program when executed by a processor implements the method of any of the above embodiments.
Compared with the prior art, the invention has the beneficial effects that:
1. quickly finishing recording; the method does not need to completely form MP4 files, but simultaneously saves key information needed in all MP4 encapsulation formats. This has the advantage that a fast end of recording can be achieved. The user needs to start recording continuously or can record next time rapidly when recording is segmented.
2. Quick power-off recovery; in fact, the recovery here does not require excessive processing. Although the last time the synchronization information was in the period between the device being powered down, the device simply continues to hold data for the media data object block without synchronization of the corresponding stbl box segment. However, since the recording process information file stores the size of the media data object block file every time the index information is synchronized, mdat data can be obtained only according to the media data object block data recorded last time.
Detailed Description
Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be embodied in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
It should be noted that, the term "first\second\ … …" related to the embodiments of the present invention is merely to distinguish similar objects, and does not represent a specific ordering for the objects, it is to be understood that "first\second\ … …" may interchange a specific order or sequence where allowed. It is to be understood that the objects identified by "first\second\ … …" may be interchanged where appropriate to enable embodiments of the invention described herein to be practiced in sequences other than those illustrated or described herein.
Example 1
Referring to fig. 1, fig. 1 is a flowchart of a method for storing a recorded mp4 file according to an embodiment of the invention, where the method includes step S110 and step S120. It should be noted that, step S110 and step S120 are only reference numerals for clearly explaining the correspondence between the embodiments and fig. 1, and do not represent a sequential limitation of the steps of the method in the present embodiment.
Step S110, when recording is started, creating a folder associated with the mp4 file to be saved on the external memory, and creating a media data object block, stbl box segment and recording process information file under the folder.
When recording is started, i.e. when a recording instruction is received, creating a folder associated with the mp4 file to be saved on the external memory, wherein the folder is used for saving relevant data finally packaged into the mp4 file. The external storage refers to storage except for computer memory and CPU cache, and such storage can still keep data after power failure. Common external memories are hard disks, floppy disks, compact disks, U disks, etc. Since the relevant material for packaging into mp4 files is placed in the folder, it is necessary to create a corresponding box according to the media file structure standard of mp 4. Thus, a media data object chunk, stbl box tile, is created under the associated folder. Wherein the media data object block records specific audio and video media data. The stbl box slices record key media information parameters of the corresponding sequence. In addition, in order to record the progress and events in the recording process, a recording process information file needs to be created.
It should be noted that, the media data generally includes audio and video data, and may include only video data or audio data according to actual needs.
stbl is the most complex box, which contains the most sub-boxes in mp4 files, and contains the necessary information to locate and decode samples. Typically recorded mp4 has two sequences of audio and video at the same time, so a set of stbl box slices needs to be created for each of the audio and video separately. Typically each set of stbl boxes is partitioned according to the respective boxes that the stbl contains, comprising 7 sub-level partitions, each sub-level partition being in fact a binary file.
stbl box contains 7 sub-level hierarchical information:
stsd: sample description box, description information of the sample.
stts: time to sample box, sample decoding time.
ctts: composition time to sample box compression table of the time difference between CTS and DTS of sample.
stss: a sync sample box, sequence number of key frames for video.
stsz/stz2: sample size box, byte size of each sample.
stsc: sample to chunk box sample-chunk mapping table.
stco/co64: the offset of the chunk in the file.
It should be noted here that there are at least two methods of creating stbl box slices, one is to create two stbl box slices for audio and video, respectively, i.e., only two stbl box slices in total; the other is to directly create 7 sub-level fragments for each set of stbl boxes, i.e. a total of 14 fragments. Of course, it is also possible to combine two ways, to create 7 sub-level fragments for one set and only stbl box fragments for the other set.
Step S120, when recording is performed, writing media data into the media data object block, and periodically updating the stbl box segment and the recording process information file.
As recording proceeds, media data is written to the media data object block and the stbl box segments are updated periodically. Since each slice under the stbl box actually records index information about each type of media data object block, but as the recording progresses, the information is growing, so that it is necessary to update the stbl box slices at regular intervals, so as to ensure that the information in the slices can be synchronously corresponding to the content in the media data object block.
At the same time, the recording process information file is updated correspondingly. Typically, the most important function of the recording process information file is to record when the content in the stbl box is updated. Because the writing of media data into a media data object block is performed at every moment during the recording process, and the updating of the content in the stbl box is performed periodically, the data of the media data object block growth in the period of time before the next update after the current update of the stbl box is not recorded with an index. At this time, it is known which part of the media data is not recorded with the index by referring to the recording process information file.
In the method, key information required in all MP4 encapsulation formats is stored while recording, and updated stbl box fragments are stored in the folder continuously, so that even if recording equipment is suddenly powered off or interrupted accidentally, the content of the stbl box fragments is stored in the folder without disappearance, after the recording equipment is restarted, when an mp4 file is to be generated, the stbl box fragments can be read into the memory from the folder to generate a metadata container moov, and then the mp4 file without damage is obtained by encapsulation according to the standard format of the mp 4.
It should be noted that the method does not need to form a complete mp4 file, but only saves the key information needed in all mp4 package formats while recording. This has the advantage that a fast end of recording can be achieved. The user needs to start recording continuously or can record next time rapidly when recording is segmented.
In one embodiment, as shown in fig. 2, the mp4 file storing method for recording further includes step S130.
And step S130, when the recording is finished, updating the stbl box fragments and the recording process information file.
In step S120, although the stbl box slice and the recording process information file are periodically updated, the time point when the user finishes recording is most likely not the updated time point, and if the stbl box slice and the recording process information file are not updated, the index information of the media data after the last update time is deleted, so that the finally obtained mp4 file does not have the content of the end portion. Therefore, when the user actively finishes recording, the stbl box fragments and the recording process information file are updated to generate a complete metadata container moov.
Here, this step is performed when the user actively ends the recording, but the case of power-off or unexpected interruption is not known, and in this case, this step is not present.
In one embodiment, the process of creating stbl box fragments under the folder includes step S210.
Step S210, for at least one group of stbl box slices, creating sub-level slices below the stbl box slices.
Since the sub-level slice under the stbl box slice is grown as the recording proceeds and periodically updated, if only one stbl box slice is created, the starting position of each sub-level slice in the stbl box slice file is changed as each sub-level slice grows, which is very inconvenient for rewriting each sub-level slice. Therefore, in the embodiment, when the sub-level fragments of the stbl box are directly created and need to be updated and rewritten, each sub-level fragment can be directly updated and rewritten, and the initial position does not need to be searched from a large file, so that the method is simpler, faster and more convenient.
In one embodiment, the mp4 file storing method for recording further includes step S140.
Step S140, after stopping recording, generating a corresponding mp4 container according to the information in the folder based on the first generation instruction when receiving the first generation instruction.
As described above, the method does not need to form a complete mp4 file, only stores the key information needed in all the mp4 package formats while recording, but generates the corresponding mp4 container according to the information in the folder in order to obtain the mp4 file smoothly. In addition, in the transmission process of the mp4 file, sometimes, not the mp4 file, but each container under the mp4 file is actually transmitted, so in the method, the set first generation instruction is only used for commanding to generate the mp4 container. In addition, in the current mp4 package format, moov is generally arranged after mdat, but sometimes for other reasons, moov is arranged before mdat, and two different setting methods can cause difference of partial information in moov, so that the first generation instruction further comprises a position relation between the two, and content of the position relation in the instruction is also needed to be based when the corresponding mp4 container is generated.
Here, the stopping recording in this step includes stopping modes in two aspects, one is stopping when the user actively ends recording, and the other is stopping when the user suddenly gets powered off or accidentally or the like.
Here, the first generation instruction may be a direct instruction or a phase-change instruction. For example, these mp4 files are all stored in the server, and when a download request is received, an mp4 container is generated, and then the download request is a phase change instruction.
In one embodiment, the process of generating the corresponding mp4 container according to the information in the folder includes: step S310, step S320, and step S330.
In step S310, a file type container ftpy is generated.
Step S320, based on the first generation instruction, generating a metadata container moov according to the stbl box fragments under the folder.
Step S330, according to the recording process information file, reads the media data object block with the specified length, and generates a media data container mdat.
Generally, mp4 files include: the three containers of the file type container ftpy, the metadata container moov and the media data container mdat are generated, and the steps S310, S320 and S330 are steps of correspondingly generating the three containers.
It should be noted here that, as described above, after the recording apparatus is suddenly stopped by a power failure or accident, it is possible to know which part of the media data is not recorded with the index by referring to the recording process information file. Since this part of the media data is not index-recorded, it should not be written into the media data container mdat, so that in step S330, a block of the media data object of a specified length is read based on the recording process information file, and only the content of the media data with the index record is put into the media data container mdat.
It should be noted that, the device for generating the media data container mdat may be a local terminal, for example, a server terminal, or may be a client terminal or a browser that requests to download.
In one embodiment, the mp4 file storing method for recording further includes step S150.
And step S150, when receiving the second generation instruction, obtaining an mp4 file through the mp4 container.
Upon receiving the second generation instruction, the mp4 file may be synthesized on the basis of the respective mp4 container.
In one embodiment, the length of a cycle is 1-3 minutes.
The setting of the period is typically a result of a combination of considerations, e.g. taking into account how long lost fragments can be tolerated after a power outage, taking into account how much resources are to be occupied for updating stbl box fragments, etc. In this embodiment, from practical point of view, it is preferable to obtain a period of 1 to 3 minutes, preferably 2 minutes.
The mp4 file storing method for recording stores key information needed in all mp4 packaging formats while recording, does not need to form a complete mp4 file, can reduce the risk of losing index information when recording equipment is powered off or interrupted accidentally, can reduce time and memory space when the mp4 file is generated in an arrangement mode, and can quickly release related resources.
Example two
Corresponding to the method of the first embodiment, as shown in fig. 3, the present invention further provides an mp4 file storing apparatus 3 for recording, including: a creation module 301 and an update module 302.
A creating module 301, configured to create a folder associated with an mp4 file to be saved on an external memory when recording is started, and create a media data object block, a stbl box partition and a recording process information file under the folder;
an updating module 302, configured to write media data into the media data object block and periodically update the stbl box segment and the recording process information file when recording is performed.
In one embodiment, the creating module 301 is further configured to update each stbl box partition and the recording process information file when recording is finished.
In one embodiment, the process of creating stbl box fragments under the folder by the creation module includes the steps of: for at least one set of stbl box slices, a sub-level slice under the stbl box slices is created.
In one embodiment, the mp4 file storing apparatus for recording further includes a generating module 303.
The generating module 303 is configured to generate, when receiving a first generating instruction after stopping recording, a corresponding mp4 container according to information in the folder based on the first generating instruction.
In one embodiment, the generating module generates the mp4 container according to the information in the folder, and the generating module includes the following steps:
generating a file type container ftpy;
generating a metadata container moov according to the stbl box fragments under the folder based on the first generation instruction;
and reading the media data object block with the designated length according to the recording process information file to generate a media data container mdat.
In one embodiment, the generating module 303 is further configured to obtain, when receiving the second generating instruction, an mp4 file through the mp4 container.
In one embodiment, the length of a cycle is 1-3 minutes.
The mp4 file storage device for recording stores key information needed in all mp4 packaging formats while recording, does not need to form a complete mp4 file, can reduce the risk of losing index information when recording equipment is powered off or interrupted accidentally, can reduce time and memory space when the mp4 file is generated in an arrangement mode, and can quickly release related resources.
Example III
The embodiment of the invention also provides a storage medium, on which computer instructions are stored, which when executed by a processor, implement the mp4 file storing method for recording of any of the above embodiments.
Those skilled in the art will appreciate that: all or part of the steps for implementing the above method embodiments may be implemented by hardware associated with program instructions, where the foregoing program may be stored in a computer readable storage medium, and when executed, the program performs steps including the above method embodiments; and the aforementioned storage medium includes: a mobile storage device, a random access Memory (RAM, random Access Memory), a Read-Only Memory (ROM), a magnetic disk or an optical disk, or the like, which can store program codes.
Alternatively, the above-described integrated units of the present invention may be stored in a computer-readable storage medium if implemented in the form of software functional modules and sold or used as separate products. Based on such understanding, the technical solution of the embodiments of the present invention may be essentially or part contributing to the related art, and the computer software product may be stored in a storage medium, and include several instructions to cause a computer device (which may be a personal computer, a terminal, or a network device) to execute all or part of the methods of the embodiments of the present invention. And the aforementioned storage medium includes: various media capable of storing program code, such as a removable storage device, RAM, ROM, magnetic or optical disk.
Corresponding to the above-mentioned computer storage medium, in one embodiment there is also provided a computer device comprising a memory, an encoder and a computer program stored on the memory and executable on the encoder, wherein the encoder implements the mp4 file storing method for recording according to any of the above-mentioned embodiments when executing the program.
The computer equipment stores the key information needed in all MP4 encapsulation formats while recording, does not need to form a complete MP4 file, can reduce the risk of losing index information when the recording equipment is powered off or interrupted accidentally, and can reduce the time and memory space when the mp4 file is generated in an arrangement mode, and quickly release related resources.
The technical features of the above-described embodiments may be arbitrarily combined, and all possible combinations of the technical features in the above-described embodiments are not described for brevity of description, however, as long as there is no contradiction between the combinations of the technical features, they should be considered as the scope of the description.
It is to be understood that the above examples of the present invention are provided by way of illustration only and not by way of limitation of the embodiments of the present invention. Other variations or modifications of the above teachings will be apparent to those of ordinary skill in the art. It is not necessary here nor is it exhaustive of all embodiments. Any modification, equivalent replacement, improvement, etc. which come within the spirit and principles of the invention are desired to be protected by the following claims.