Media file generation method and device

By obtaining and adding metadata of track code streams and media data blocks during the MP4 format media file generation process, and generating media information packets when abnormal termination is generated, the problem of abnormal termination of the MP4 format media file generation process is solved, and the decodeable media file is generated.

CN120075546APending Publication Date: 2025-05-30BEIJING ZITIAO NETWORK TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311611549.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-11-29
Publication Date
2025-05-30

AI Technical Summary

Technical Problem

Media files in MP4 format are abnormally terminated during the generation process, resulting in the generated files being unable to be decoded, affecting the user experience.

Method used

During the MP4 format media file generation process, metadata corresponding to the code stream of each track and metadata of the media data block are obtained. When the generation process is abnormally terminated, a media information packet is generated based on these metadata and added to the file to be repaired to obtain the decodeable media file.

Benefits of technology

By generating and adding media information packages, we ensure that the final generated media file contains the necessary metadata, so that it can be decoded normally, solving the problem that the file cannot be decoded due to abnormal interruption of the generation process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120075546A_ABST
    Figure CN120075546A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a media file generation method and device, and relates to the technical field of data processing. The method comprises the steps of obtaining metadata corresponding to code streams of all tracks of a media file and metadata corresponding to all media data blocks of the media file in the generation process of the media file in the MP4 format; adding the metadata corresponding to the code stream of each track and the metadata corresponding to each media data block into a media file; when the generation process of the media file is abnormally broken, metadata corresponding to the code stream of each track and metadata corresponding to each media data block are obtained according to a to-be-repaired file, and the to-be-repaired file is the media file stored when the generation process of the media file is abnormally broken; generating a media information packet of the to-be-repaired file according to the metadata corresponding to the code stream of each track and the metadata corresponding to each media data block; and adding the media information packet of the to-be-repaired file into the to-be-repaired file to obtain a first media file.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the technical field of data processing, and in particular, to a method and apparatus for generating a media file. Background Art

[0002] MP4 is a widely used compression and encoding standard for audio and video information, developed by the Moving Picture Experts Group (MPEG) under the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC).

[0003] The media file in MP4 format includes a media information box (moov box) for storing file metadata. When decoding a media file, the media data in the media file needs to be accessed based on the file metadata in the media information box. Therefore, the media file in MP4 format must contain a media information box to be decoded normally. However, since the generation of the media information box is the last step in the generation process of the MP4 format media file, the MP4 format media file often does not include a media information box due to an abnormal interruption in the generation process, resulting in the inability to decode the generated file. This unexpected interruption greatly affects the user experience, especially in scenarios where MP4 format media files are generated for a long time, such as recording. Summary of the Invention

[0004] In view of this, embodiments of this application provide a method and apparatus for generating a media file, which are used to solve the problem that an abnormal interruption in the generation process of an MP4 format media file may cause the generated file to be unable to be decoded.

[0005] To achieve the above object, the embodiments of this application provide the following technical solutions:

[0006] In a first aspect, an embodiment of this application provides a method for generating a media file, including:

[0007] During the generation process of an MP4 format media file, obtaining the metadata corresponding to the bitstreams of each track of the media file and the metadata corresponding to each media data block of the media file;

[0008] Adding the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file;

[0009] When the generation process of the media file is abnormally terminated, obtain the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to the file to be repaired, where the file to be repaired is the media file saved when the generation process of the media file is abnormally terminated;

[0010] Generate a media information packet of the file to be repaired according to the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block;

[0011] Add the media information packet of the file to be repaired to the file to be repaired to obtain a first media file.

[0012] As an optional implementation manner of an embodiment of the present application, adding the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file includes:

[0013] Generate information blocks corresponding to the bitstreams of each track according to the metadata corresponding to the bitstreams of each track;

[0014] Add the information blocks corresponding to the bitstreams of each track to the head end of the media data packet of the media file.

[0015] As an optional implementation manner of an embodiment of the present application, adding the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file includes:

[0016] Add the metadata corresponding to the media data block to the data block header of the corresponding media data block.

[0017] As an optional implementation manner of an embodiment of the present application, the method further includes: during the generation process of the media file, set the file header packet of the media file to a preset file header packet;

[0018] The obtaining the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to the file to be repaired includes:

[0019] When the file header packet of the file to be repaired is the preset file header packet, obtain the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to the file to be repaired.

[0020] As an optional implementation manner of an embodiment of the present application, the method further includes:

[0021] When the generation process of the media file ends normally, set the file header packet of the second media file according to the MP4 standard;

[0022] Wherein, the second media file is the media file saved when the generation process of the media file ends normally.

[0023] As an optional implementation manner of the embodiment of the present application,

[0024] The metadata corresponding to the bitstream includes at least one of media data description information STSD, media header information MDHD, and track header information TKHD;

[0025] The metadata corresponding to the media data block includes at least one of media data key frame information STSS, media data decoding time information STTS, media data rendering time information CTTS, media data block offset information STCO, and media frame number information STSC.

[0026] As an optional implementation manner of the embodiment of the present application, generating the media information packet of the file to be repaired according to the metadata corresponding to the bitstream of each track and the metadata corresponding to each media data block includes:

[0027] Generating a media data list file STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block and STSD in the metadata corresponding to the bitstream to which the media data block belongs;

[0028] Generating a media information file MINF corresponding to each media data block according to the STBL corresponding to each media data block;

[0029] Generating a media track structure file MDIA for each bitstream according to the MINF corresponding to each media data block and HDMD in the metadata corresponding to the bitstream to which the media data block belongs;

[0030] Generating a track information file TRAK corresponding to each bitstream according to the MDIA of each bitstream and the TKHD of each bitstream;

[0031] Generating the media information packet according to the TRAK of each bitstream.

[0032] As an optional implementation manner of the embodiment of the present application, generating a media data list file STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block and STSD in the metadata corresponding to the bitstream to which the media data block belongs includes:

[0033] Obtaining the media data volume information STZS of each media data block according to the quantity of the media data in each media data block;

[0034] Generate a media data list file STBL for each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block, STSD in the metadata corresponding to the bitstream to which the media data block belongs, and STZS of each media data block.

[0035] In a second aspect, an embodiment of the present application provides a media file generation device, including:

[0036] A generation unit, configured to obtain metadata corresponding to the bitstreams of each track of the media file and metadata corresponding to each media data block of the media file during the generation process of the media file in MP4 format;

[0037] An adding unit, configured to add metadata corresponding to the bitstreams of each track and metadata corresponding to each media data block to the media file;

[0038] An obtaining unit, configured to obtain metadata corresponding to the bitstreams of each track and metadata corresponding to each media data block according to a file to be repaired when the generation process of the media file is abnormally terminated, where the file to be repaired is a media file saved when the generation process of the media file is abnormally terminated;

[0039] A repairing unit, configured to generate a media information packet of the file to be repaired according to metadata corresponding to the bitstreams of each track and metadata corresponding to each media data block, and add the media information packet of the file to be repaired to the file to be repaired to obtain a first media file.

[0040] As an optional implementation manner of an embodiment of the present application, the adding unit is specifically configured to generate information blocks corresponding to the bitstreams of each track according to metadata corresponding to the bitstreams of each track; and add the information blocks corresponding to the bitstreams of each track to the head end of the media data packet of the media file.

[0041] As an optional implementation manner of an embodiment of the present application, the adding unit is specifically configured to add metadata corresponding to the media data block to the data block header of the corresponding media data block.

[0042] As an optional implementation manner of an embodiment of the present application, the generation unit is further configured to set the file header packet of the media file to a preset file header packet during the generation process of the media file;

[0043] The obtaining unit is specifically configured to obtain metadata corresponding to the bitstreams of each track and metadata corresponding to each media data block according to the file to be repaired when the file header packet of the file to be repaired is the preset file header packet.

[0044] As an optional implementation manner of an embodiment of the present application, the repair unit is further configured to set a file header packet of a second media file according to the MP4 standard when the generation process of the media file ends normally;

[0045] Wherein, the second media file is a media file saved when the generation process of the media file ends normally.

[0046] As an optional implementation manner of an embodiment of the present application,

[0047] The metadata corresponding to the bitstream includes at least one of media data description information STSD, media header information MDHD, and track header information TKHD;

[0048] The metadata corresponding to the media data block includes at least one of media data key frame information STSS, media data decoding time information STTS, media data rendering time information CTTS, media data block offset information STCO, and media frame number information STSC.

[0049] As an optional implementation manner of an embodiment of the present application, the repair unit is specifically configured to generate a media data list file STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block and STSD in the metadata corresponding to the bitstream to which the media data block belongs; generate a media information file MINF corresponding to each media data block according to the STBL corresponding to each media data block; generate a media track structure file MDIA for each bitstream according to the MINF corresponding to each media data block and HDMD in the metadata corresponding to the bitstream to which the media data block belongs; generate a track information file TRAK corresponding to each bitstream according to the MDIA of each bitstream and TKHD of each bitstream; and generate the media information packet according to the TRAK of each bitstream.

[0050] As an optional implementation manner of an embodiment of the present application, the repair unit is specifically configured to obtain media data volume information STZS of each media data block according to the quantity of media data in each media data block; and generate a media data list file STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block, STSD in the metadata corresponding to the bitstream to which the media data block belongs, and STZS of each media data block.

[0051] In a third aspect, an embodiment of the present application provides an electronic device, including: a memory and a processor. The memory is used to store a computer program, and the processor is used to cause the electronic device to implement the method for generating a media file according to any one of the above embodiments when executing the computer program.

[0052] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, which causes a computing device to implement the method for generating a media file according to any one of the above when the computer program is executed by the computing device.

[0053] In a fifth aspect, an embodiment of the present application provides a computer program product, which causes a computer to implement the method for generating a media file according to any one of the above when the computer program product runs on the computer.

[0054] The method for generating a media file provided by the embodiment of the present application can obtain the metadata corresponding to the bitstreams of each track of the media file and the metadata corresponding to each media data block of the media file during the generation process of the media file in MP4 format, and add the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file. Therefore, when the generation process of the media file is abnormally terminated, the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block can be obtained according to the to-be-repaired file saved when the generation process is abnormally terminated, generate a media information packet of the to-be-repaired file according to the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block, and add the media information packet of the to-be-repaired file to the to-be-repaired file to obtain a first media file. Since the first media file obtained by repair includes a media information packet, the first media file can be normally decoded. Therefore, the embodiment of the present application can solve the problem that the generated file cannot be decoded due to the abnormal termination of the generation process of the media file in MP4 format. BRIEF DESCRIPTION OF THE DRAWINGS

[0055] The accompanying drawings herein are incorporated into the specification and constitute a part of the specification, showing embodiments consistent with the present application, and are used together with the specification to explain the principles of the present application.

[0056] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the accompanying drawings required to be used in the description of the embodiments or the prior art. Obviously, for those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0057] Figure 1 One of the schematic diagrams of the media file in MP4 format provided by the embodiment of the present application;

[0058] Figure 2Schematic diagram of the structure of the media information packet provided by the embodiment of the present application;

[0059] Figure 3 One of the flowcharts of the method for generating a media file provided by the embodiment of the present application;

[0060] Figure 4 Another flowchart of the method for generating a media file provided by the embodiment of the present application;

[0061] Figure 5 Schematic diagram of the structure of the information block corresponding to the bitstream provided by the embodiment of the present application;

[0062] Figure 6 Another schematic diagram of the media file in MP4 format provided by the embodiment of the present application;

[0063] Figure 7 Another schematic diagram of the media file in MP4 format provided by the embodiment of the present application;

[0064] Figure 8 Schematic diagram of the repair process of the file to be repaired provided by the embodiment of the present application;

[0065] Figure 9 Schematic diagram of the structure of the media file generation device provided by the embodiment of the present application;

[0066] Figure 10 Schematic diagram of the hardware structure of the electronic device provided by the embodiment of the present application. Detailed implementation manners

[0067] In order to more clearly understand the above objects, features and advantages of the present application, the solutions of the present application will be further described below. It should be noted that, without conflict, the embodiments of the present application and the features in the embodiments can be combined with each other.

[0068] In the following description, many specific details are set forth to facilitate a thorough understanding of the present application, but the present application may also be implemented in other ways different from those described herein. Obviously, the embodiments in the specification are only a part of the embodiments of the present application, rather than all the embodiments.

[0069] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or explanations. Any embodiment or design solution described as "exemplary" or "for example" in the embodiments of the present application should not be construed as being more preferred or having more advantages than other embodiments or design solutions. Exactly speaking, the use of words such as "exemplary" or "for example" is intended to present relevant concepts in a specific manner. In addition, in the description of the embodiments of the present application, unless otherwise stated, the meaning of "a plurality of" refers to two or more.

[0070] This application relates to the generation of media files in MP4 format. The following first describes the media file structure in MP4 format.

[0071] Refer to Figure 1 As shown, the media file in MP4 format consists of a file header box (ftyp box) 100, a media data box (mdat box) 200, and a media information box (moov box) 300.

[0072] The file header box 100 includes information such as type, size, major_brand, major_version, and compatible_brands, which are used to declare the specifications followed by the file.

[0073] The media data box 200 is used to store media data compressed into binary. The media data can include video data, audio data, and subtitles, etc. The media data compressed into binary is stored in the media data box 200 in the form of multiple media data blocks (Block / Sample).

[0074] Refer to Figure 2 As shown, the first-layer file of the media information box 300 includes track information files (Track box, TRAK) corresponding to the bitstreams of each track of the media file. Figure 2Taking TRAK1, TRAK2, ……, TRAKn as examples. Any track information file includes: a track media structure file (Track Media Structure, MDIA) and a track header information file (Track Header Box, TKHD). The track media structure file includes: a media header information file (Media Header Box, MDHD) and a media information file (Media Information Box, MINF). The media information file includes: a media data list file (Sample Table Box, STBL). The media data list file includes: media data block size (Sample Size Boxes, STSZ), media data description information (Sample Description Box, STSD), media data key frame information (Sync Sample Box, STSS), media data decoding time information (DecodingTime to Sample Box, STTS), media data rendering time information (Composition Time to Sample Box, CTTS), media data block offset information (Chunk Offset Box, STCO), and media frame number information (Sample ToChunk Box, STSC), etc. files. The media information packet 300 is used to store the metadata of the media file.

[0075] An embodiment of the present application provides a method for generating a media file. Refer to Figure 3 As shown, the method for generating the media file includes the following steps S31 to S35:

[0076] S31. During the generation process of an MP4-format media file, obtain the metadata corresponding to the bitstreams of each track of the media file and the metadata corresponding to each media data block of the media file.

[0077] In some embodiments, the metadata corresponding to the bitstream includes at least one of STSD, MDHD, and TKHD.

[0078] Among them, STSD is used to declare the description information of the media data in the bitstream and contains all the initialization information required in the decoding stage. For example: the resolution of the video, the depth information of the bitmap, etc. In addition, since the initialization information required for video and audio is different, the content of STSD of the bitstream of the video track and the content of STSD of the bitstream of the audio track also differ.

[0079] MDHD is used to declare general information such as the start time, revision time, duration, time unit, and language used of the bitstream.

[0080] TKHD is used to declare information such as the creation time, most recent modification time, identifier, full duration, stacking order, audio volume, video transformation matrix, and video size of the bitstream.

[0081] In some embodiments, the metadata corresponding to the media data blocks includes at least one of STSS, STTS, CTTS, STCO, and STSC.

[0082] Among them, STSS is used to declare the index of the media data block where the key frame is located; STT contains a mapping table of the decoding time stamp (DTS) and the index of the media data block, which can be used to deduce the duration of each frame, describes the mapping method of the media data block timing, and any media data block corresponding to a time can be found through STSS; CTTS is used to declare the difference between the decoding time stamp and the rendering time stamp; STCO is used to declare the offset of the media data block in the media file; one or more media data blocks form a media data group, and STSC is used to declare the number of media data blocks included in the media data group.

[0083] S32. Add the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file.

[0084] In the embodiments of the present application, the implementation manner of adding the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file is not limited, and it is subject to the media file including the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block.

[0085] It should be noted that the metadata corresponding to each media data block needs to be obtained in real time during the generation process of the media file. Whenever a media data block is generated based on video or audio data, the metadata corresponding to the media data block can be obtained, and the metadata corresponding to the media data block is added to the media file.

[0086] S33. When the generation process of the media file is abnormally terminated, obtain the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to the file to be repaired.

[0087] Among them, the file to be repaired is the media file saved when the generation process of the media file is abnormally terminated.

[0088] In the embodiments of the present application, the reason for the abnormal interruption of the generation process of the media file is not limited. Regardless of the reason for the abnormal interruption of the generation process of the media file, the above step S33 will be triggered to execute. Exemplarily, the reasons for the abnormal interruption of the generation process of the media file may be: power failure of the media file generation device, damage to the media file generation device, software error of the media file generation device, etc.

[0089] Based on the above steps S31 and S32, it can be known that during the generation process of the media file, the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block are added to the media file. Therefore, the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block can be read from the media file saved at the time of abnormal interruption.

[0090] S34. Generate a media information packet (moov box) of the file to be repaired according to the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block.

[0091] Since the media information packet is the metadata of the media file, and its main content is the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block, the media information packet of the file to be repaired can be generated according to the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block.

[0092] S35. Add the media information packet of the file to be repaired to the file to be repaired to obtain a first media file.

[0093] Since the generation process of the media file is abnormally interrupted, and in the generation process of the MP4 format media file, the media information packet is the last step in the process, and the media information packet is generated only when the generation of the media file is completed. Therefore, the file to be repaired saved at the time of abnormal interruption of the generation process of the media file does not include the media information packet and cannot be decoded normally. In the embodiments of the present application, the media information packet of the file to be repaired is generated through the above steps S33 to S35, and the media information packet of the file to be repaired is added to the file to be repaired. Therefore, the obtained first media file can be decoded normally.

[0094] The method for generating a media file provided by an embodiment of the present application can, during the generation process of a media file in MP4 format, obtain the metadata corresponding to the bitstreams of each track of the media file and the metadata corresponding to each media data block of the media file, and add the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file. Therefore, when the generation process of the media file is abnormally terminated, the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block can be obtained according to the to-be-repaired file saved when the generation process is abnormally terminated, a media information packet of the to-be-repaired file can be generated according to the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block, and the media information packet of the to-be-repaired file can be added to the to-be-repaired file to obtain a first media file. Since the first media file obtained by repair includes a media information packet, the first media file can be normally decoded. Therefore, the embodiment of the present application can solve the problem that the generated file cannot be decoded due to the abnormal termination of the generation process of a media file in MP4 format.

[0095] As an extension and refinement of the above embodiment, an embodiment of the present application provides another method for generating a media file. Referring to Figure 4 shown, the method for generating the media file includes the following steps:

[0096] S401. During the generation process of a media file in MP4 format, obtain the metadata corresponding to the bitstreams of each track of the media file and the metadata corresponding to each media data block of the media file.

[0097] Among them, the metadata corresponding to the bitstream includes at least one of STSD, MDHD, and TKHD; the metadata corresponding to the media data block includes at least one of STSS, STTS, CTTS, STCO, and STSC.

[0098] S402. Generate information blocks corresponding to the bitstreams of each track according to the metadata corresponding to the bitstreams of each track.

[0099] Referring to Figure 5 shown, in some embodiments, the information block corresponding to the bitstream may include: an information header (Header) 51 and an information body (Body) 52. Among them, the information header 51 includes fields such as type, size, and stream ID, and the STSD, the MDHD, and the TKHD may be stored in the information body 52.

[0100] S403. Add the information blocks corresponding to the bitstreams of each track to the head end of the media data packet of the media file.

[0101] Exemplarily, referring toFigure 6 As shown, when the media file includes the bitstreams of two tracks, after adding the information blocks corresponding to the bitstreams of each track to the head end of the media data packet of the media file, the media file includes: a file header packet (ftyp box) 100, a media data packet (mdat box) 200, and a media information packet (moov box) 300, and the first two data blocks of the media data packet 200 are respectively the first information block 61 and the second information block 62 corresponding to the bitstreams of the two tracks.

[0102] S404. Add the metadata corresponding to the media data block to the data block header of the corresponding media data block.

[0103] Refer to Figure 7 As shown, in some embodiments, the media data block may include: a data block header 71 and a data block body 72. After adding the metadata corresponding to the media data block to the data block header of the corresponding media data block, the data block header 71 includes fields such as data block size, data block type, stream ID, STSS, STTS, CTTS, STCO, and STSC, and the video or audio data is stored in the data block body 72.

[0104] S405. When the generation process of the media file is abnormally terminated, obtain the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to the file to be repaired.

[0105] Wherein, the file to be repaired is the media file saved when the generation process of the media file is abnormally terminated.

[0106] S406. Generate the STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block and STSD in the metadata corresponding to the bitstream to which the media data block belongs.

[0107] In some embodiments, generating the STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block and STSD in the metadata corresponding to the bitstream to which the media data block belongs includes: obtaining the media data volume information STZS of each media data block according to the quantity of the media data in each media data block; and generating the media data list file STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block, STSD in the metadata corresponding to the bitstream to which the media data block belongs, and the STZS of each media data block.

[0108] S407. Generate MINF corresponding to each media data block according to the STBL corresponding to each media data block.

[0109] S408. Generate MDIA for each bitstream according to the MINF corresponding to each media data block and the HDMD in the metadata corresponding to the bitstream to which it belongs.

[0110] S409. Generate TRAK corresponding to each bitstream according to the MDIA of each bitstream and the TKHD of each bitstream.

[0111] S410. Generate the media information packet according to the TRAK of each bitstream.

[0112] That is, with reference to Figure 8 As shown, generating the media information of the file to be repaired according to the metadata corresponding to the bitstream of each track and the metadata corresponding to each media data block includes: First, obtain the STZS of the media data block according to the data volume of the media data block, and then generate STBL 81 corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block, STSD in the metadata corresponding to the bitstream to which the media data block belongs, and the STZS of each media data block. Second, generate MINF 82 corresponding to each media data block according to STBL 81 corresponding to each media data block. Third, generate MDIA 83 for each bitstream according to MINF 82 corresponding to each media data block and the HDMD in the metadata corresponding to the bitstream to which the media data block belongs. Fourth, generate the track information file TRAK 84 corresponding to each bitstream according to MDIA 83 of each bitstream and the TKHD of each bitstream. Finally, generate the media information packet 300 according to TRAK 84 of each bitstream.

[0113] S411. Add the media information packet of the file to be repaired to the file to be repaired to obtain the first media file.

[0114] In some embodiments, the method for generating a media file provided by the embodiments of the present application further includes: during the generation process of the media file, set the file header packet of the media file to a preset file header packet; in the case where the file header packet of the file to be repaired is the preset file header packet, obtain the metadata corresponding to the bitstream of each track and the metadata corresponding to each media data block according to the file to be repaired.

[0115] That is, modify the file header of the media file to a file header that can identify the file to be repaired, so as to determine whether repair can be performed according to the file header of the media file subsequently.

[0116] Exemplarily, setting the file header packet of the media file to a preset file header packet includes: adding a preset identifier to the file header packet, setting the value of a specified field in the file header packet to a preset value, setting each field in the file header packet to be empty, etc. The embodiments of the present application do not limit this, and it is subject to that the preset file header packet can identify the media file as a file to be repaired.

[0117] In some embodiments, the method for generating a media file provided by the embodiments of the present application further includes: after adding the media information packet of the file to be repaired to the file to be repaired to obtain a first media file, setting the file header packet of the first media file according to the MP4 standard.

[0118] Setting the file header packet of the first media file according to the MP4 standard after obtaining the first media file can, while identifying the file to be repaired, ensure the normal decoding of the first media file.

[0119] In some embodiments, the method for generating a media file provided by the embodiments of the present application further includes: in the case where the generation process of the media file ends normally, setting the file header packet of the second media file according to the MP4 standard;

[0120] wherein, the second media file is the media file saved when the generation process of the media file ends normally.

[0121] Similarly, in the case where the generation process of the media file ends normally, setting the file header packet of the second media file according to the MP4 standard can, while identifying the file to be repaired, ensure the normal decoding of the second media file.

[0122] Based on the same inventive concept, as an implementation of the above method, the embodiments of the present application further provide a device for generating a media file. This embodiment corresponds to the foregoing method embodiment. For the convenience of reading, the details in the foregoing method embodiment will not be described one by one in this embodiment, but it should be clear that the device for generating a media file in this embodiment can correspondingly implement all the contents in the foregoing method embodiment.

[0123] The embodiments of the present application provide a device for generating a media file, Figure 9 as shown in the structural schematic diagram of the device for generating this media file, Figure 9 as shown, the device 900 for generating a media file includes:

[0124] A generating unit 91, configured to obtain metadata corresponding to the bitstreams of each track of the media file and metadata corresponding to each media data block of the media file during the generation process of the media file in the MP4 format;

[0125] An adding unit 92 is configured to add metadata corresponding to the bitstreams of each track and metadata corresponding to each media data block to the media file;

[0126] A obtaining unit 93 is configured to, when the generation process of the media file is abnormally terminated, obtain metadata corresponding to the bitstreams of each track and metadata corresponding to each media data block according to a file to be repaired, where the file to be repaired is a media file saved when the generation process of the media file is abnormally terminated;

[0127] A repairing unit 94 is configured to generate a media information packet of the file to be repaired according to metadata corresponding to the bitstreams of each track and metadata corresponding to each media data block, and add the media information packet of the file to be repaired to the file to be repaired to obtain a first media file.

[0128] As an optional implementation manner of an embodiment of this application, the adding unit 92 is specifically configured to generate information blocks corresponding to the bitstreams of each track according to metadata corresponding to the bitstreams of each track; and add the information blocks corresponding to the bitstreams of each track to the head end of the media data packet of the media file.

[0129] As an optional implementation manner of an embodiment of this application, the adding unit 92 is specifically configured to add metadata corresponding to the media data block to the data block header of the corresponding media data block.

[0130] As an optional implementation manner of an embodiment of this application, the generating unit 91 is further configured to set the file header packet of the media file to a preset file header packet during the generation process of the media file;

[0131] The obtaining unit 93 is specifically configured to obtain metadata corresponding to the bitstreams of each track and metadata corresponding to each media data block according to the file to be repaired when the file header packet of the file to be repaired is the preset file header packet.

[0132] As an optional implementation manner of an embodiment of this application, the repairing unit 94 is further configured to set the file header packet of a second media file according to the MP4 standard when the generation process of the media file ends normally;

[0133] where the second media file is a media file saved when the generation process of the media file ends normally.

[0134] As an optional implementation manner of an embodiment of this application,

[0135] The metadata corresponding to the bitstream includes at least one of media data description information STSD, media header information MDHD, and track header information TKHD;

[0136] The metadata corresponding to the media data block includes at least one of: media data key frame information STSS, media data decoding time information STTS, media data rendering time information CTTS, media data block offset information STCO, and media frame quantity information STSC.

[0137] As an optional implementation manner of an embodiment of the present application, the repair unit 94 is specifically configured to generate a media data list file STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block and STSD in the metadata corresponding to the bitstream to which the media data block belongs; generate a media information file MINF corresponding to each media data block according to the STBL corresponding to each media data block; generate a media track structure file MDIA for each bitstream according to the MINF corresponding to each media data block and HDMD in the metadata corresponding to the bitstream to which the media data block belongs; generate a track information file TRAK corresponding to each bitstream according to the MDIA of each bitstream and TKHD of each bitstream; and generate the media information packet according to the TRAK of each bitstream.

[0138] As an optional implementation manner of an embodiment of the present application, the repair unit 94 is specifically configured to obtain media data amount information STZS of each media data block according to the quantity of media data in each media data block; and generate a media data list file STBL corresponding to each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block, STSD in the metadata corresponding to the bitstream to which the media data block belongs, and STZS of each media data block.

[0139] The media file generation device provided in the embodiment of the present application can execute the media file generation method provided in any of the above embodiments, and its implementation principle and technical effect are similar, which will not be elaborated here.

[0140] Based on the same inventive concept, the embodiment of the present application further provides an electronic device. Figure 10 The structural schematic diagram of the electronic device provided in the embodiment of the present application is as Figure 10 shown. The electronic device provided in this embodiment includes: a memory 101 and a processor 102. The memory 101 is used to store a computer program, and the processor 102 is used to execute the media file generation method provided in the above embodiment when executing the computer program.

[0141] Based on the same inventive concept, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the computing device is enabled to implement the method for generating a media file provided in the above embodiment.

[0142] Based on the same inventive concept, an embodiment of the present application further provides a computer program product. When the computer program product runs on a computer, the computing device is enabled to implement the method for generating a media file provided in the above embodiment.

[0143] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media that contain computer-usable program code.

[0144] The processor can be a central processing unit (CPU), or can also be other general-purpose processors, digital signal processors (DSPs), application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor can be a microprocessor, or the processor can also be any conventional processor, etc.

[0145] The memory may include non-permanent memory in the computer-readable medium, random access memory (RAM), and / or non-volatile memory in the form of, for example, read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0146] Computer-readable media include both permanent and non-permanent, removable and non-removable storage media. The storage media can implement information storage by any method or technology, and the information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette tapes, disk storage or other magnetic storage devices, or any other non-transmission media that can be used to store information accessible by a computing device. As defined herein, computer-readable media do not include transitory computer-readable media, such as modulated data signals and carrier waves.

[0147] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements for some or all of the technical features. These modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for generating a media file, characterized in that, comprising: During the generation process of an MP4 - formatted media file, obtaining the metadata corresponding to the bitstreams of each track of the media file and the metadata corresponding to each media data block of the media file; Adding the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file; When the generation process of the media file is abnormally terminated, obtaining the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to a file to be repaired, where the file to be repaired is the media file saved when the generation process of the media file is abnormally terminated; Generating a media information packet of the file to be repaired according to the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block; Adding the media information packet of the file to be repaired to the file to be repaired to obtain a first media file.

2. The method according to claim 1, characterized in that, The step of adding the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file includes: Generating information blocks corresponding to the bitstreams of each track according to the metadata corresponding to the bitstreams of each track; Adding the information blocks corresponding to the bitstreams of each track to the head of the media data packet of the media file.

3. The method according to claim 2, characterized in that, The step of adding the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file includes: Adding the metadata corresponding to the media data block to the data block header of the corresponding media data block.

4. The method according to claim 1, characterized in that, The method further includes: during the generation process of the media file, setting the file header packet of the media file to a preset file header packet; The step of obtaining the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to the file to be repaired includes: When the file header packet of the file to be repaired is the preset file header packet, obtaining the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to the file to be repaired.

5. The method according to claim 4, characterized in that, The method further includes: When the generation process of the media file ends normally, setting the file header packet of a second media file according to the MP4 standard; wherein, the second media file is the media file saved when the generation process of the media file ends normally.

6. The method according to any one of claims 1 - 5, characterized in that, The metadata corresponding to the bitstream includes at least one of: media data description information STSD, media header information MDHD, and track header information TKHD; The metadata corresponding to the media data block includes at least one of: media data key frame information STSS, media data decoding time information STTS, media data rendering time information CTTS, media data block offset information STCO, and media frame number information STSC.

7. The method according to claim 6, characterized in that, Generating the media information packet of the file to be repaired based on the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block includes: Generating a media data list file STBL for each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block and STSD in the metadata corresponding to the bitstream to which the media data block belongs; Generating a media information file MINF for each media data block according to the STBL corresponding to each media data block; Generating a media track structure file MDIA for each bitstream according to the MINF corresponding to each media data block and HDMD in the metadata corresponding to the bitstream to which the media data block belongs; Generating a track information file TRAK for each bitstream according to the MDIA of each bitstream and TKHD of each bitstream; Generating the media information packet according to the TRAK of each bitstream.

8. The method according to claim 7, wherein, generating a media data list file STBL for each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block and STSD in the metadata corresponding to the bitstream to which the media data block belongs includes: Obtaining the media data volume information STZS of each media data block according to the number of media data in each media data block; Generating a media data list file STBL for each media data block according to STSS, STTS, CTTS, STCO, STSC in the metadata corresponding to each media data block, STSD in the metadata corresponding to the bitstream to which the media data block belongs, and STZS of each media data block.

9. A generating device for a media file, wherein, it includes: A generating unit, configured to obtain the metadata corresponding to the bitstreams of each track of the media file and the metadata corresponding to each media data block of the media file during the generation process of the media file in MP4 format; An adding unit, configured to add the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block to the media file; An obtaining unit, configured to obtain the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block according to the file to be repaired when the generation process of the media file is abnormally terminated, where the file to be repaired is the media file saved when the generation process of the media file is abnormally terminated; A repairing unit, configured to generate the media information packet of the file to be repaired according to the metadata corresponding to the bitstreams of each track and the metadata corresponding to each media data block, and add the media information packet of the file to be repaired to the file to be repaired to obtain a first media file.

10. An electronic device, wherein, it includes: A memory and a processor, the memory is used to store a computer program, and the processor is configured to cause the electronic device to implement the media file generation method according to any one of claims 1-8 when executing the computer program.

11. A computer-readable storage medium, It is characterized in that a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a computing device, the computing device realizes the method for generating a media file according to any one of claims 1-8.