Audio and video recording method and related device
By precalculating the media information box size of the audio and video file and writing it to the free box after the file type box, the problem of not being able to write the media information box in time under abnormal conditions during audio and video recording is solved, and effective recording and saving in abnormal conditions is achieved, ensuring the integrity and playability of the file.
Patent Information
- Application Number
- CN202411841379.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-11
AI Technical Summary
During the audio and video recording process, the prior art cannot write the media information box in time under abnormal conditions, resulting in the missing index information of the MP4 file and the normal playback is not possible.
By obtaining the recording parameters of the initial audio and video file, data is written into the file type box, and the size of the media information box is pre-calculated, and an idle box of corresponding size is written after the file type box. In response to the recorded audio and video data, the data of the media information box and the media data box are written in the free box.
It realizes effective recording and saving of MP4 files in abnormal situations, ensuring their integrity and playability, improving the efficiency of audio and video recording and reducing resource occupancy costs.
Smart Images

Figure CN119814949A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of audio and video recording, and in particular to an audio and video recording method and related devices. Background Art
[0002] With the continuous development of multimedia technology, MP4 file format, as a widely used multimedia box format, has been widely used in the fields of audio and video recording, storage and playback. In MP4 files, the media information box is a key data structure, which contains the index information of the media file, including the decoding parameters and timeline information of the audio and video streams.
[0003] During the audio and video recording process, due to the continuous increase in the amount of recorded data, the content in the media information box needs to be continuously updated as the recording progresses. However, in order to avoid storage performance degradation or data corruption caused by frequent writing, existing recording solutions usually choose to write the entire media information box at one time after the recording is completed. Although this method improves recording performance, it also brings greater risks: if abnormal situations such as power outage, memory card removal, or platform damage occur during the recording process, the media information box fails to write the complete content in time, which may cause the MP4 file to lack necessary index information and cannot be played normally.
[0004] Therefore, it is necessary to propose an efficient and resource-saving audio and video recording solution that can effectively record and save MP4 files under abnormal circumstances and ensure their integrity and playability. Summary of the invention
[0005] The main technical problem solved by the present application is to provide an audio and video recording method and related devices, which can improve the efficiency of audio and video recording, reduce resource occupation costs, and thus effectively record and save MP4 files under abnormal circumstances and ensure their integrity and playability.
[0006] In order to solve the above technical problems, a technical solution adopted in this application is: the method comprises:
[0007] Obtain recording parameters of the initial audio and video file, and write data of the file type box into the initial audio and video file;
[0008] Pre-calculating the size of the media information box based on the recording parameters, so as to write a free box corresponding to the size of the media information box after the file type box;
[0009] In response to the recorded audio and video data, the data of the media information box is written into the free box, and the data of the media data box is written after the free box to obtain the target audio and video file.
[0010] Optionally, in response to the recorded audio and video data, writing the data of the media information box in the free box, and writing the data of the media data box after the free box, to obtain the target audio and video file, comprises:
[0011] In response to the recorded audio and video data, storing the audio and video data in a cache buffer;
[0012] In response to the data in the cache buffer satisfying the preset time length, the data of the media information box is written from the starting position of the free box according to the data in the cache buffer, the size of the free box is updated according to the data size of the media information box, and the data of the media data box is written after the free box;
[0013] This cycle is repeated until there is no data in the cache buffer, and the target audio and video file is obtained.
[0014] Optionally, in response to the recorded audio and video data, storing the audio and video data in a cache buffer includes:
[0015] Acquire audio and video data; wherein the audio and video data includes audio data and video data;
[0016] Encoding and encapsulating the audio data to obtain data of the media information box; and encoding and encapsulating the video data to obtain data of the media data box;
[0017] storing the data of the media information box and the data of the media data box in the cache buffer;
[0018] The process repeats this cycle until the recording of the audio and video data stops.
[0019] Optionally, writing the data of the media information box from the starting position of the free box includes:
[0020] The preset offset file pointer is moved to the starting position of the free box, and the data of the media information box is written from the starting position of the free box.
[0021] Optionally, precalculating the size of the media information box based on the recording parameters includes:
[0022] Obtaining default parameters of various sub-boxes under the media information box;
[0023] The size of the media information box is calculated by combining the recording parameters and the default parameters.
[0024] Optionally, the calculating the size of the media information box by combining the recording parameter and the default parameter includes:
[0025] Combining the recording parameters and the default parameters, calculating a size range of the media information box;
[0026] A current recording scene is determined, and a size of the media information box is determined within a size range of the media information box according to the current recording scene.
[0027] Optionally, the recording parameters include at least one of the recording duration, encoding format, sampling rate and frame rate of the audio and video file; the default parameters include at least one of the key frame interval corresponding to the audio and video data, the number of block data frames, the name field length of the seed box, the variable field length, and the number of entries.
[0028] In order to solve the above technical problems, another technical solution adopted by the present application is: an audio and video recording device, the audio and video recording device comprising:
[0029] A first acquisition module is used to acquire recording parameters of an initial audio and video file and write data of a file type box into the initial audio and video file;
[0030] A first estimation module, configured to precalculate the size of the media information box based on the recording parameters, so as to write a free box corresponding to the size of the media information box after the file type box;
[0031] The first writing module is used to write the data of the media information box in the free box and write the data of the media data box after the free box in response to the recorded audio and video data, so as to obtain the target audio and video file.
[0032] To solve the above technical problems, another technical solution adopted in the present application is: to provide an electronic device, which includes a memory and a processor coupled to the memory, the memory stores at least one computer program, and when the at least one computer program is loaded and executed by the processor, it is used to implement the method provided by the above technical solution.
[0033] In order to solve the above technical problems, another technical solution adopted by the present application is: providing a computer-readable storage medium / computer program product, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the method provided by the above technical solution;
[0034] The computer program product includes a computer program, and when the computer program is executed by a processor, the method provided by the above technical solution is implemented.
[0035] Different from the prior art, the embodiment of the present application provides an audio and video recording method and related device, which obtains the recording parameters of the initial audio and video file, and writes the data of the file type box into the initial audio and video file; based on the recording parameters, pre-calculates the size of the media information box to write a free box of the size corresponding to the media information box after the file type box; in response to the recorded audio and video data, writes the data of the media information box in the free box, and writes the data of the media data box after the free box to obtain the target audio and video file. The size of the media information box is pre-calculated, and the free box of the corresponding size is used as the placeholder area of the media information box, which improves the efficiency of audio and video recording and reduces the resource occupation cost, so that the MP4 file can be effectively recorded and saved under abnormal circumstances and its integrity and playability can be guaranteed. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] Figure 1 It is a flowchart of the first embodiment of the audio and video recording method of the present application;
[0037] Figure 2 It is a flowchart of the second embodiment of the audio and video recording method of the present application;
[0038] Figure 3 This is a schematic diagram of the composition of the audio and video file encapsulation format box of the audio and video recording method of the present application;
[0039] Figure 4 It is a schematic diagram of the application scenario of the audio and video recording method of the present application;
[0040] Figure 5a to Figure 5d It is a schematic diagram of the change of the audio and video file packaging format of the audio and video recording method of the present application;
[0041] Figure 6 It is a structural schematic diagram of an embodiment of an electronic device in the present application;
[0042] Figure 7 It is a schematic diagram of the structure of an embodiment of a computer-readable storage medium in the present application;
[0043] Figure 8 It is a structural diagram of an embodiment of a computer program product in the present application. DETAILED DESCRIPTION
[0044] The present application is described in detail below with reference to the accompanying drawings and implementation methods.
[0045] See also Figure 1 , Figure 1 1 is a flowchart of the first embodiment of the audio and video recording method of the present application. It should be noted that if there is substantially the same result, the method of the present application is not limited to the first embodiment of the audio and video recording method of the present application. Figure 1 The process sequence shown is limited. Figure 1 As shown, the method includes:
[0046] Step S110, obtaining recording parameters of the initial audio and video file, and writing data of the file type box into the initial audio and video file;
[0047] The audio and video file may be at least one of an MP4 file, a movie MOV file, and an F4V file. The recording parameters may be some basic information that needs to be determined at the beginning of recording the audio and video file, such as recording duration, audio sampling rate, video frame rate, audio encoding format, audio encoding format, etc. The recording parameters are used to determine the storage format and decoding method of the audio and video data. The file type box may be the ftyp box format in the MP4 file; the file type box may be the first part of the data structure of the audio and video file, which is used to identify the basic format and meta information of the file. The file type box may include a file format identifier, file version information, a file compatibility declaration, etc. The data written into the file type box may initialize the audio and video file structure and make the audio and video file identifiable, such as when playing and transmitting across platforms and multiple devices.
[0048] Step S120, precalculating the size of the media information box based on the recording parameters, so as to write a free box corresponding to the size of the media information box after the file type box;
[0049] Among them, the media information box can be a moov box in an MP4 file, which is used to store metadata information related to audio and video streams, which may include the timing, frame index, sampling description, etc. of audio and video tracks. Precalculation can refer to estimating the storage space required for the media information box based on recording parameters and box parameters of the media information box. The free box can be a free box, which is a placeholder area that does not store any actual audio or video data and is used to temporarily occupy a storage location, indicating that this part of the space is logically reserved and ensures that the file format is physically continuous, so that the data of the media information box can write actual data into the free box, avoiding repeated adjustments to the structure of the audio and video files during the recording process.
[0050] Step S130, in response to the recorded audio and video data, write the data of the media information box into the free box, and write the data of the media data box after the free box to obtain the target audio and video file.
[0051] Specifically, the recorded audio and video data refers to the actual audio and video stream data collected by the recording device. During the recording process, the information of the audio and video frames will be updated to the media information box in real time or after the recording is completed, such as frame timestamp, frame size, track information, etc. The media data box can be the mdat box in the MP4 file, which is used to store the actual encoded data of the audio and video. The data of all boxes are integrated together to finally generate a complete file that meets the specifications. Ensure that the recorded audio and video data is correctly associated with the metadata to facilitate decoding and playback.
[0052] This embodiment precalculates the size of the media information box, uses a free box of a corresponding size as a placeholder area of the media information box, and writes the data of the media information box into the placeholder area, thereby improving the efficiency of audio and video recording and reducing resource occupation costs.
[0053] See also Figure 2 , Figure 2 This is a flow chart of the second embodiment of the audio and video recording method of the present application. The method comprises the following steps:
[0054] Step S210, obtaining recording parameters of the initial audio and video file, and writing data of the file type box into the initial audio and video file;
[0055] The above step S110 may be performed and will not be described in detail herein.
[0056] Step S220, obtaining default parameters of various sub-boxes under the media information box;
[0057] In an exemplary embodiment, Figure 3 As shown, the MP4 encapsulation format is mainly composed of an ftyp box, a moov box, and an mdat box. The moov box can be a box for storing media-related information of an MP4 file, and is composed of header information and multiple seed boxes. According to the MP4 specification, the size of the header information is a fixed value of 8 bytes. The moov box can contain the following multiple seed boxes, at least one of an mvhd box, a trak box, a tkhd box, an edts box, an mdia box, an mdhd box, an hdlr box, a minf box, a vmhd box, an smhd box, a dinf box, an stbl box, a stsd box, a stts box, a ctts box, a stss box, a stsz box, a stsc box, and a stco / co64 box.
[0058] The corresponding sizes of each seed box are as follows:
[0059] The mvhd box may be in accordance with the MP4 specification. When the version information is set to 0, the size is a fixed value of 108 bytes; when the version information is set to 1, the size is a fixed value of 120 bytes.
[0060] The trak box can be composed of header information and multiple sub-boxes. According to the MP4 specification, the size of the trak box header information is a fixed value of 8 bytes.
[0061] According to the MP4 specification, when the version information is set to 0, the size of the tkhd box is a fixed value of 92 bytes; when the version information is set to 1, the size is a fixed value of 104 bytes.
[0062] The edts box can be a box for storing the timestamp offset information of the track. If there is no offset, this edts box is not needed; if there is an offset, the number of timestamp offset entries can be obtained when the timestamp of the first frame stream data is obtained. According to the mp4 specification, when the version information is set to 0 or 1, the corresponding edts box size is shown in Table 1 below.
[0063] Table 1
[0064]
[0065] The mdia box can be composed of header information and multiple sub-boxes. According to the MP4 specification, the size of the mdia box header information is a fixed value of 8 bytes.
[0066] The mdhd box can be in accordance with the MP4 specification. When the version information is set to 0, the size is a fixed value of 32 bytes; when the version information is set to 1, the size is a fixed value of 44 bytes.
[0067] The hdlr box can be a box for storing track types. According to the MP4 specification, the size of the name field is variable. According to the data type setting, the corresponding hdlr box size is shown in Table 2 below.
[0068] Table 2
[0069]
[0070] The minf box can be composed of header information and multiple sub-boxes. According to the MP4 specification, the size of the minf box header information is a fixed value of 8 bytes.
[0071] The vmhd box may be a fixed size of 20 bytes as specified in the mp4 specification.
[0072] The SMHD box may be sized as a fixed value of 16 bytes in accordance with the MP4 specification.
[0073] The dinf box may be a box that describes how to locate media information. According to the MP4 specification, when recording to a local file, the flag of the url subbox of the dinf box should be set to 1, so the size is a fixed value of 36 bytes. This box may not be set.
[0074] The stbl box can be composed of header information and multiple sub-boxes. According to the MP4 specification, the size of the stbl box header information is a fixed value of 8 bytes.
[0075] The stsd box can be a box that describes the encoding type and the information required by the decoder. According to the mp4 specification, it is composed of fixed parameters and variable parameters. The variable parameters are determined by different encoding types. Before recording, the video variable parameter size is set to the variable parameter size of the video, and the audio variable parameter size is set to the variable parameter size of the audio. Therefore, the stsd box size of the video is fixed (102 + variable parameter size of the video) bytes, and the stsd box size of the audio is fixed (52 + variable parameter size of the audio) bytes, as shown in Table 3 below.
[0076] Table 3
[0077]
[0078] The stts box can be a box that describes time increment information. According to the MP4 specification, its size is determined by the number of entries N (number of entries) of the time increment, and the size is fixed to 16+8N (number of entries), where N (number of entries) has a minimum value of 1 and a maximum value of the total number of data frames. Therefore, the stts box size of the video ranges from 24 bytes to (16+8*total number of video frames) bytes, and the stts box size of the audio ranges from 24 bytes to (16+8*total number of audio frames) bytes, as shown in Table 4 below.
[0079] Table 4
[0080]
[0081] The ctts box can be a box that describes the coding time offset. According to the MP4 specification, its size is determined by the number of entries N (number of entries) of the coding time offset, and the size is fixed to 16+8N (number of entries), and the maximum value is the total number of data frames. If there is no coding time offset, this box may not exist. Therefore, the size of the ctts box for video ranges from 0 bytes to (16+8*total number of video frames) bytes, and the size of the ctts box for audio ranges from 0 bytes to (16+8*total number of audio frames) bytes, as shown in Table 5 below.
[0082] Table 5
[0083]
[0084] The stss box can be a box that describes key frame information. According to the MP4 specification, its size is determined by the total number of key frames. By setting encoding parameters, the encoder can encode a key frame according to the key frame interval seconds of each video. Therefore, the size of the stss box of the video is fixed to 16+(recording time / key frame interval of the video)*4. This box is not required for audio.
[0085] The stsz box can be a box that describes the size information of each frame of data. According to the MP4 specification, its size is determined by the total number of data frames. Therefore, the stsz box size of the video is fixed (20*+total number of video frames*4) bytes, and the stsz box size of the audio is fixed (20*+total number of audio frames*4) bytes.
[0086] The stsc box can be a box that describes the mapping relationship between frame data and block data. According to the MP4 specification, its size is determined by the mapping relationship between frame data and block data. By setting the file recording to store a fixed number of frame data in a block data, only two entries need to be retained, and the size is fixed to 40 bytes. If a one-to-one mapping relationship is adopted, the maximum number of entries is the total number of data frames. Therefore, the size range of the stsc box for video is 40 bytes to (16+12*total number of video frames) bytes, and the size range of the stts box for audio is 40 bytes to (16+12*total number of audio frames) bytes, as shown in Table 6 below.
[0087] Table 6
[0088]
[0089]
[0090] The stco / co64 box can be a box that describes the offset position information of the block data. There are two forms to choose from, namely the stco box and the co64 box. According to the mp4 specification, its size is determined by the total number of block data. The minimum value is stored as a fixed number of frame data contained in a block data, and the maximum value is stored as a block data containing only one frame data. Therefore, the sizes of the stco box and the co64 box are shown in Table 7 below.
[0091] Table 7
[0092]
[0093] The sizes of the seed boxes of the moov box are obtained from the above analysis, as shown in Tables 8 and 9 below:
[0094] Table 8
[0095]
[0096]
[0097] Table 9
[0098]
[0099]
[0100] Step S230, combining the recording parameters and the default parameters, calculating the size of the media information box, and writing a free box corresponding to the size of the media information box after the file type box;
[0101] Specifically, the default parameters may include static parameters and dynamic parameters under various sub-boxes. For example, the fixed part size of the media information box may be calculated based on the static parameters, and the size of the variable part may be estimated based on the dynamic parameters, and the required size of the media information box may be obtained by combining the recording parameters for calculation, and then the corresponding free box may be generated based on this size.
[0102] In some embodiments, the calculating the size of the media information box by combining the recording parameters and the default parameters may include: calculating the size range of the media information box by combining the recording parameters and the default parameters; determining a current recording scene, and determining the size of the media information box within the size range of the media information box according to the current recording scene.
[0103] In an exemplary embodiment, the size of the moov box is pre-calculated according to the recording parameters, and the free box data of the pre-calculated size of the moov box is written into the file. The cyclic file writing mechanism is started, and the actual moov box data is written to the initial starting position of the free box at regular intervals, and the mdat box data is written to the position after the free box. Among them, according to the recording time (in seconds) and the frame rate of the video, the total number of video frames can be obtained = the frame rate of the video * the recording time; at the same time, according to the recording time (in seconds), the sampling rate of the audio, and the number of audio samples contained in one frame in the audio encoding format, the total number of audio frames can be obtained = the sampling rate of the audio * the recording time / the number of audio samples.
[0104] According to the preset default parameter settings, the calculation formula for the moov box size can be obtained as follows:
[0105] The minimum value of the moov box size is: minimum moov box size = 906 + hdlr box name size of the video + variable parameter size of the video + 4*(recording time / key frame interval of the video) + 4*total number of video frames + 4*total number of video frames / number of block data frames of the video + hdlr box name size of the audio + variable parameter size of the audio + 4*total number of audio frames + 4*total number of audio frames / number of block data frames of the audio;
[0106] The maximum value of the moov box size is: maximum moov box size = 1038 + 20 * number of elst entries of the video + hdlr box name size of the video + variable parameter size of the video + 40 * total number of frames of the video + 4 * (recording time / key frame interval of the video) + 20 * number of elst entries of the audio + hdlr box name size of the audio + variable parameter size of the audio + 40 * total number of frames of the audio.
[0107] This embodiment takes into account that different recording scenes may cause changes in recording parameters. Therefore, the above formula in this embodiment and the preset default parameters can make corresponding internal adjustments according to different recording scenes, so that the actual moov box size recorded is not greater than the pre-calculated value obtained by the above formula. Among them, the recording scene can be a scene of changing light conditions, different action intensities, recording duration, resolution requirements, slow motion recording, etc.
[0108] In some embodiments, the recording parameters include at least one of the recording duration, encoding format, sampling rate and frame rate of the audio and video files; the default parameters include at least one of the key frame interval corresponding to the audio and video data, the number of block data frames, the name field length of the seed box, the variable field length, and the number of entries.
[0109] In an exemplary embodiment, the hdlr box name size of the video may be the hdlr box name field length set according to the video encoding format; the variable parameter size of the video may be the stsd box variable field length set according to the video encoding format, the recording time may be a preset total recording time in seconds; the key frame interval of the video may be a preset number of seconds for the encoder to produce a key frame; the number of block data frames of the video may be a preset number of frames of data contained in a fixed block of video data; the hdlr box name size of the audio may be the hdlr box name field length set according to the audio encoding format; the variable parameter size of the audio may be the stsd box variable field length set according to the audio encoding format; the number of block data frames of the audio may be a preset number of frames of data contained in a fixed block of audio data; the number of elst entries of the video may be the number of entries in the edts box set according to the timestamp of the first video data; the number of elst entries of the audio may be the number of entries in the edts box set according to the timestamp of the first audio data.
[0110] Step S240, in response to the recorded audio and video data, storing the audio and video data in a cache buffer;
[0111] Specifically, since writing directly to a storage device may cause a bottleneck, such as when the disk writing speed is slower than the data acquisition speed, a circular or fixed-size cache area can be set up, which can be used to temporarily store audio and video data. The data is stored in order through a queue management mechanism to ensure data consistency.
[0112] In some embodiments, step S240 may include: obtaining audio and video data; wherein the audio and video data includes audio data and video data; encoding and packaging the audio data to obtain data of the media information box; and encoding and packaging the video data to obtain data of the media data box; storing the data of the media information box and the data of the media data box in the cache buffer; and repeating this cycle until the recording of the audio and video data stops.
[0113] Specifically, the amount of data in the cache buffer of the media data box can be monitored, and when the data content reaches 500ms, the file writing mechanism is started; then, according to the corresponding specification standard of mp4, the actual media information box data and size are obtained; then, the actual media information box data is written to the initial starting position of the free box, the media data box data is written to the position of the media data box, and the free box is updated again.
[0114] Step S250, in response to the data in the cache buffer satisfying the preset time length, writing the data of the media information box from the starting position of the free box according to the data in the cache buffer, updating the size of the free box according to the data size of the media information box, and writing the data of the media data box after the free box;
[0115] Specifically, since the free box is used as a placeholder, it needs to be continuously filled with actual data and its size updated as the recording progresses. The starting position and remaining size of the free box are adjusted by calculating the size of the filled data. Then the recorded data is extracted from the cache, the data in the cache buffer is written in frame or timestamp order, and the corresponding data is generated according to the format of the media information box and written into the media information box.
[0116] In some embodiments, writing the data of the media information box from the starting position of the free box may include: moving a preset offset file pointer to the starting position of the free box, and writing the data of the media information box from the starting position of the free box.
[0117] Specifically, the offset file pointer can be used to specify the location of the current audio and video file reading and writing. Since the size of the free box varies according to the size of the media information box, more specifically, the size of the free box will decrease accordingly as the media information box increases, the offset file pointer can be used to jump to the starting position of the free box, thereby ensuring that the data written to the next media information box will not overwrite the data area written to the media information box.
[0118] Step S260, repeat this cycle until there is no data in the cache buffer, and obtain the target audio and video file.
[0119] Specifically, recording can be a continuous process, which can ensure that the file structure and content are intact when recording is interrupted. Check whether the data in the cache meets the preset duration or size threshold to trigger the write operation and then clear the cache to continue receiving new data. When there is no data in the cache buffer, it means that the recording is complete. The final generated audio and video file format is complete, including both metadata (media information box) and content data (media data box).
[0120] In an exemplary embodiment, see Figure 4 and Figure 5a-5d First, create an MP4 file and write a free box of the same size as the moov box to the file after the ftyp box. Figure 5a The audio and video file data structure shown;
[0121] If the recording is not stopped, the following steps are executed to process the audio data:
[0122] Acquire audio data through a microphone; send the audio data to an encoder for encoding; acquire the encoded audio data from the encoder; encapsulate the encoded audio data into an mdat box according to the mp4 encapsulation format; store the encapsulated mdat box in an mdat box cache buffer.
[0123] At the same time, if the recording is not stopped, the steps of video data processing are executed in a loop:
[0124] Acquire video data through a camera; send the video data to an encoder for encoding; acquire the encoded video data from the encoder; encapsulate the encoded video data into an mdat box according to the mp4 encapsulation format; store the encapsulated mdat box in an mdat box cache buffer.
[0125] While looping the video data processing and audio data processing, if the recording is not stopped, monitor the amount of data in the mdat box cache buffer. When the data content reaches 500ms, loop the file writing mechanism:
[0126] See also Figure 5b and Figure 5c, get the actual moov box data and size according to the corresponding specification to update the size of the moov box, and the free box decreases as the moov box increases; then, offset the file pointer to the initial starting position of the free box, and write the data in the mdat box cache buffer into the moov box; at the same time, write the data in the cache buffer into the mdat box accordingly.
[0127] When there is no data in the mdat box cache buffer, the recording is terminated directly, and the following is obtained: Figure 5d The audio and video file data structure is shown.
[0128] In this embodiment, a free box mechanism with the same size as the media information box is written after the file type box, and the audio and video data is written into the cache buffer using a loop mechanism. This can effectively realize the real-time dynamic generation of file content and structure, while reducing the burden on the storage device. It also supports interrupt recovery during the audio and video file generation process to ensure data security.
[0129] In addition, the embodiment of the present application further provides an audio and video recording device, the audio and video recording device comprising:
[0130] A first acquisition module is used to acquire recording parameters of an initial audio and video file and write data of a file type box into the initial audio and video file;
[0131] A first estimation module, configured to precalculate the size of the media information box based on the recording parameters, so as to write a free box corresponding to the size of the media information box after the file type box;
[0132] The first writing module is used to write the data of the media information box in the free box and write the data of the media data box after the free box in response to the recorded audio and video data, so as to obtain the target audio and video file.
[0133] Optionally, the first writing module includes:
[0134] A first storage module, in response to the recorded audio and video data, stores the audio and video data in a cache buffer;
[0135] a second writing module, configured to, in response to the data in the cache buffer satisfying a preset time length, write the data of the media information box from the starting position of the free box according to the data in the cache buffer, update the size of the free box according to the data size of the media information box, and write the data of the media data box after the free box;
[0136] The first loop module is used to loop in this way until there is no data in the cache buffer, thereby obtaining the target audio and video file.
[0137] Optionally, the first storage module includes:
[0138] A second acquisition module is used to acquire audio and video data; wherein the audio and video data includes audio data and video data;
[0139] An encoding and encapsulation module, used to encode and encapsulate the audio data to obtain the data of the media information box; and to encode and encapsulate the video data to obtain the data of the media data box;
[0140] A second storage module, used for storing the data of the media information box and the data of the media data box into the cache buffer;
[0141] The second loop module is used to loop in this way until the recording of the audio and video data stops.
[0142] Optionally, the second writing module includes:
[0143] The third writing module is used to move the preset offset file pointer to the starting position of the free box, and write the data of the media information box from the starting position of the free box.
[0144] Optionally, the first estimation module includes:
[0145] A third acquisition module, used to acquire default parameters of various sub-boxes under the media information box;
[0146] The second estimation module is used to calculate the size of the media information box by combining the recording parameters and the default parameters.
[0147] Optionally, the second estimation module includes:
[0148] A third estimation module, configured to calculate a size range of the media information box by combining the recording parameters and the default parameters;
[0149] The determination module is used to determine the current recording scene, and determine the size of the media information box within the size range of the media information box according to the current recording scene.
[0150] Optionally, the recording parameters include at least one of the recording duration, encoding format, sampling rate and frame rate of the audio and video file; the default parameters include at least one of the key frame interval corresponding to the audio and video data, the number of block data frames, the name field length of the seed box, the variable field length, and the number of entries.
[0151] See also Figure 6 , Figure 61 is a schematic diagram of the structure of an embodiment of an electronic device in the present application. The electronic device 60 includes a memory 61 and a processor 62 coupled to the memory 61. The memory 61 stores at least one computer program. When the at least one computer program is loaded and executed by the processor 62, it is used to implement the following method:
[0152] Acquire recording parameters of an initial audio and video file, and write data of a file type box into the initial audio and video file; pre-calculate the size of a media information box based on the recording parameters, so as to write a free box of a size corresponding to the size of the media information box after the file type box; in response to the recorded audio and video data, write data of the media information box into the free box, and write data of the media data box after the free box, so as to obtain a target audio and video file.
[0153] It can be understood that when at least one computer program is loaded and executed by the processor 62, it is also used to implement the method of any of the above embodiments.
[0154] See also Figure 7 , Figure 7 1 is a schematic diagram of a computer-readable storage medium in an embodiment of the present application. The computer-readable storage medium 70 has a computer-executable instruction 71, which is used to implement the following method when loaded and executed by a processor:
[0155] Acquire recording parameters of an initial audio and video file, and write data of a file type box into the initial audio and video file; pre-calculate the size of a media information box based on the recording parameters, so as to write a free box of a size corresponding to the size of the media information box after the file type box; in response to the recorded audio and video data, write data of the media information box into the free box, and write data of the media data box after the free box, so as to obtain a target audio and video file.
[0156] It can be understood that when the computer execution instruction 71 is loaded and executed by the processor, it is used to implement the method of any of the above embodiments.
[0157] See also Figure 8 , Figure 8 80 is a schematic diagram of a computer program product in an embodiment of the present application. The computer program product 80 includes a computer program 81, and when the computer program 81 is executed by a processor, it is used to implement the following method:
[0158] Acquire recording parameters of an initial audio and video file, and write data of a file type box into the initial audio and video file; pre-calculate the size of a media information box based on the recording parameters, so as to write a free box of a size corresponding to the size of the media information box after the file type box; in response to the recorded audio and video data, write data of the media information box into the free box, and write data of the media data box after the free box, so as to obtain a target audio and video file.
[0159] It can be understood that when the computer program 81 is loaded and executed by the processor, it is used to implement the method of any of the above embodiments.
[0160] The above scheme obtains the recording parameters of the initial audio and video file, and writes the data of the file type box into the initial audio and video file; based on the recording parameters, pre-calculates the size of the media information box to write a free box of the size corresponding to the media information box after the file type box; in response to the recorded audio and video data, writes the data of the media information box into the free box, and writes the data of the media data box after the free box to obtain the target audio and video file. By pre-calculating the size of the media information box, using the free box of the corresponding size as the placeholder area of the media information box, and writing the data of the media information box into the placeholder area, the efficiency of audio and video recording can be improved and the resource occupation cost can be reduced.
[0161] In the several embodiments provided in the present application, it should be understood that the disclosed methods, electronic devices and storage media can be implemented in other ways. For example, the device implementation described above is only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0162] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0163] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0164] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) or a processor (processor) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program code.
[0165] The above description is only an implementation method of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly used in other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. An audio and video recording method, characterized in that: The audio and video recording method comprises: Obtain recording parameters of the initial audio and video file, and write data of the file type box into the initial audio and video file; Pre-calculating the size of the media information box based on the recording parameters, so as to write a free box corresponding to the size of the media information box after the file type box; In response to the recorded audio and video data, the data of the media information box is written into the free box, and the data of the media data box is written after the free box to obtain the target audio and video file.
2. The audio and video recording method according to claim 1, characterized in that: The step of writing the data of the media information box in the free box in response to the recorded audio and video data, and writing the data of the media data box after the free box to obtain the target audio and video file comprises: In response to the recorded audio and video data, storing the audio and video data in a cache buffer; In response to the data in the cache buffer satisfying the preset time length, the data of the media information box is written from the starting position of the free box according to the data in the cache buffer, the size of the free box is updated according to the data size of the media information box, and the data of the media data box is written after the free box; This cycle is repeated until there is no data in the cache buffer, and the target audio and video file is obtained.
3. The audio and video recording method according to claim 2, characterized in that: The step of storing the recorded audio and video data in a cache buffer in response to the recorded audio and video data comprises: Acquire audio and video data; wherein the audio and video data includes audio data and video data; Encoding and encapsulating the audio data to obtain data of the media information box; and encoding and encapsulating the video data to obtain data of the media data box; storing the data of the media information box and the data of the media data box in the cache buffer; The process repeats this cycle until the recording of the audio and video data stops.
4. The audio and video recording method according to claim 2, characterized in that: The step of writing the data of the media information box from the starting position of the free box comprises: The preset offset file pointer is moved to the starting position of the free box, and the data of the media information box is written from the starting position of the free box.
5. The audio and video recording method according to claim 1, characterized in that: The precalculating the size of the media information box based on the recording parameters includes: Obtaining default parameters of various sub-boxes under the media information box; The size of the media information box is calculated by combining the recording parameters and the default parameters.
6. The audio and video recording method according to claim 5, characterized in that: The calculating the size of the media information box by combining the recording parameter and the default parameter includes: Combining the recording parameters and the default parameters, calculating a size range of the media information box; A current recording scene is determined, and a size of the media information box is determined within a size range of the media information box according to the current recording scene.
7. The audio and video recording method according to claim 6, characterized in that: The recording parameters include at least one of the recording duration, encoding format, sampling rate and frame rate of the audio and video files; The default parameters include at least one of a key frame interval corresponding to the audio and video data, the number of block data frames, the name field length of the seed box, the variable field length, and the number of entries.
8. An audio and video recording device, characterized in that: The audio and video recording device comprises: A first acquisition module is used to acquire recording parameters of an initial audio and video file and write data of a file type box into the initial audio and video file; A first estimation module, configured to precalculate the size of the media information box based on the recording parameters, so as to write a free box corresponding to the size of the media information box after the file type box; The first writing module is used to write the data of the media information box in the free box and write the data of the media data box after the free box in response to the recorded audio and video data, so as to obtain the target audio and video file.
9. An electronic device, characterized in that: The electronic device includes a memory and a processor coupled to the memory, the memory stores at least one computer program, and when the at least one computer program is loaded and executed by the processor, it is used to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium / computer program product, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 7 when executed by a processor; The computer program product comprises a computer program, which implements the method according to any one of claims 1 to 7 when executed by a processor.