Video cutting method and device and electronic equipment
Patent Information
- Application Number
- CN202311172521.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-11
- Publication Date
- 2025-03-11
AI Technical Summary
[0003]本申请实施方式主要解决现有的视频裁剪方式效率低以及准确性低的技术问题
[0037] Different from the relevant technology, the embodiment of the present application provides a video cutting method, device and electronic device. The method obtains the keyframe of the video to be cropped, and divides the video to be cropped into a first cropped segment and a second cropped segment based on the keyframe, so that the keyframe becomes the first frame of the second cropped segment, and then re-encodes the first cropped segment to obtain the first video, copy the second cropped segment to obtain the second video, and then splices the first video and the second video to obtain the cropped video. The part that needs to be recoded during this process is very short, the whole method takes a short time, and the video cropping efficiency is very high. In addition, the first frame of the video cropped by this method must be an I frame, so it is relatively compatible with all players, the display time in the player is accurate, and the player does not need to calculate the decoded first frame is a B frame or a P frame, so it avoids the performance differences between different players.
Smart Images

Figure CN119603499A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer application technology, and in particular to a video cropping method, device and electronic device. Background Art
[0002] With the rapid development of multimedia and the Internet, video has become an indispensable way for users to obtain information in their lives. Therefore, the demand for video editing has become more and more widespread. Common video cropping methods, such as directly copying the video to be cropped, usually do not use an I frame (intra-frame coded frame) as the first frame. In this case, the PTS (display timestamp) of the first frame will mostly be negative, resulting in inaccurate video duration displayed by some players, and the displayed duration will be greater than the actual duration of the video. Another common video cropping method is to re-encode the video to be cropped, which takes a certain amount of time, especially for high-definition videos or long videos to be cropped. It takes a lot of time to re-encode the video. Summary of the invention
[0003] The implementation methods of the present application mainly solve the technical problems of low efficiency and low accuracy of existing video cropping methods.
[0004] In order to solve the above technical problems, a technical solution adopted in the implementation mode of the present application is: to provide a video cropping method, the method comprising:
[0005] Acquire a key frame of the video to be cropped, and divide the video to be cropped into a first cropping segment and a second cropping segment based on the key frame, so that the key frame becomes the first frame of the second cropping segment;
[0006] Re-encoding the first cropped segment to obtain a first video;
[0007] Copying the second cropped segment to obtain a second video;
[0008] The first video and the second video are spliced to obtain a cropped video.
[0009] Optionally, obtaining key frames of the video to be cropped includes:
[0010] Determine the start time of the video to be cropped, and determine the first frame of the video to be cropped according to the start time;
[0011] If the first frame is not an I frame, traverse the video to be cropped and take the first I frame as the key frame;
[0012] If the first frame is an I frame, the first frame is used as the key frame, or the video to be cropped is traversed and the second I frame is used as the key frame.
[0013] Optionally, if the first frame of the video to be cropped is not a key frame, dividing the video to be cropped into a first cropped segment and a second cropped segment based on the key frame includes:
[0014] Determine the first frame to the key frame of the video to be cropped as the first cropped segment;
[0015] Determine the second cropped segment from the key frame to the last frame of the video to be cropped.
[0016] Optionally, the re-encoding the first cropped segment further includes:
[0017] Get the most recent I frame before the key frame;
[0018] The first cropped segment is re-encoded in conjunction with the most recent I frame.
[0019] Optionally, if the first frame of the video to be cropped is a key frame, dividing the video to be cropped into a first cropped segment and a second cropped segment based on the key frame further includes:
[0020] The video to be cropped is regarded as the second cropped segment.
[0021] Optionally, the stitching the first video and the second video to obtain a cropped video includes:
[0022] splicing the first video and the second video according to the key frames;
[0023] Adjust the timestamps of the cropped video.
[0024] In order to solve the above technical problems, another technical solution adopted in the embodiment of the present application is: to provide a video cropping device, comprising:
[0025] A video segmentation module, configured to obtain a key frame of the video to be cropped, and divide the video to be cropped into a first cropped segment and a second cropped segment based on the key frame, so that the key frame becomes a first frame of the second cropped segment;
[0026] A first processing module, configured to re-encode the first cropped segment to obtain a first video;
[0027] A second processing module, used for copying the second cropped segment to obtain a second video;
[0028] The video stitching module is used to stitch the first video and the second video to obtain a cropped video.
[0029] Optionally, the video segmentation module is further used for:
[0030] Determine the start time of the video to be cropped, and determine the first frame of the video to be cropped according to the start time;
[0031] If the first frame is not an I frame, traverse the video to be cropped and take the first I frame as the key frame;
[0032] If the first frame is an I frame, the first frame is used as the key frame, or the video to be cropped is traversed and the second I frame is used as the key frame.
[0033] In order to solve the above technical problems, another technical solution adopted in the embodiment of the present application is: to provide an electronic device, characterized in that it includes:
[0034] at least one processor;
[0035] a memory communicatively coupled to the at least one processor;
[0036] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the video cropping method described above.
[0037] Different from the related art, the embodiments of the present application provide a video cropping method, device and electronic device. The method obtains the key frame of the video to be cropped, and divides the video to be cropped into a first cropped segment and a second cropped segment based on the key frame, so that the key frame becomes the first frame of the second cropped segment, and then re-encodes the first cropped segment to obtain the first video, copies the second cropped segment to obtain the second video, and then splices the first video and the second video to obtain the cropped video. The part that needs to be re-encoded in this process is very short, the whole method is very short, and the efficiency of video cropping is very high. In addition, the first frame of the video cropped by this method must be an I frame, so it has good compatibility with all players, the display duration in the player is accurate, and the player does not need to calculate the decoding of the first frame to determine whether it is a B frame or a P frame, thereby avoiding performance differences between different players. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] One or more embodiments are exemplarily described by corresponding drawings, which do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and the figures in the drawings do not constitute proportional limitations unless otherwise stated.
[0039] Figure 1 It is a flowchart of a video cropping method provided in an embodiment of the present application;
[0040] Figure 2 is an example diagram of dividing a video to be cropped provided in an embodiment of the present application;
[0041] Figure 3 is a structural schematic diagram of a video cropping device provided in an embodiment of the present application;
[0042] Figure 4 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0043] In order to make the purpose, technical scheme and advantages of the present application clearer, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. It should be noted that, if there is no conflict, the various features in the embodiments of the present application can be combined with each other, all within the protection scope of the present application. In addition, although the functional modules are divided in the device schematic diagram and the logical order is shown in the flow chart, in some cases, the steps shown or described can be performed in a different order from the module division in the device schematic diagram or the flow chart.
[0044] Unless otherwise defined, all technical and scientific terms used in this specification have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used in the specification of this application are only for the purpose of describing specific embodiments and are not intended to limit this application. The term "and / or" used in this specification includes any and all combinations of one or more of the related listed items.
[0045] The embodiment of the present application provides a video cropping method to address the deficiencies in existing video cropping methods. Specifically, existing video cropping methods, such as directly copying the video to be cropped, usually do not use an I frame (intra-frame coded frame) as the first frame's PTS (display timestamp) will be a negative number, resulting in inaccurate video duration displayed by some players, because the display duration of some players = the PTS of the last frame - the PTS of the first frame. If the PTS of the first frame is a negative number, then in some players, the display duration of the video will be greater than the actual duration of the video. Or, for example, the video to be cropped is re-encoded, and re-encoding takes a certain amount of time, especially for high-definition videos or longer videos to be cropped, which require a lot of time to re-encode the video. Based on this, the embodiment of the present application proposes the following solution:
[0046] In the embodiment of the present application, cropping a video to be cropped from a certain video is used as an example for explanation. Figure 1, the embodiment of the present application provides a video cropping method, comprising:
[0047] S11. Acquire a key frame of a video to be cropped, and divide the video to be cropped into a first cropping segment and a second cropping segment based on the key frame, so that the key frame becomes a first frame of the second cropping segment.
[0048] Specifically, obtaining the key frame of the video to be cropped includes: determining the start time of the video to be cropped, and determining the first frame of the video to be cropped according to the start time. Figure 2 The video to be cropped in the embodiment of the present application refers to Figure 2 The actual cropping time is represented by , that is, the video portion to be cropped. The start time of the video to be cropped refers to the second at which the user wants to start cropping in the original video. Figure 2 In the example, the first frame corresponding to the start time of the video to be cropped is recorded as P_1. Similarly, Figure 2 In the example of , the last frame corresponding to the end time of the video to be cropped is recorded as P_2.
[0049] If the first frame is not an I frame, traverse the video to be cropped and use the first I frame as the key frame; if the first frame is an I frame, use the first frame as the key frame, or traverse the video to be cropped and use the second I frame as the key frame.
[0050] It can be understood that the video frames in a video can usually be divided into I frames, P frames and B frames, among which I frames represent key frames, which can be understood as the complete preservation of this frame, that is, it contains a complete picture. When decoding the video, only the data of this frame is needed to complete the decoding of this frame; P frame represents difference frame, which represents the difference between this frame and the previous key frame (or P frame), that is, P frame does not have complete picture data, but only data of picture difference with the previous frame. When decoding the video, it is necessary to superimpose the previously cached picture on the difference defined by this frame to generate the final picture corresponding to this frame; B frame represents bidirectional difference frame, which records the difference between this frame and the previous and next frames, which is somewhat similar to P frame. When decoding the video, it is necessary to obtain not only the previous cached picture but also the picture after decoding for this frame, and obtain the final picture corresponding to this frame by superimposing the previous and next pictures with the data of this frame.
[0051] At the same time, if the first frame of a video is an I frame, then its PTS (display timestamp) is 0. If the first frame of a video is not an I frame, such as a P frame or a B frame, then its PTS will generally be a negative number, which will cause the video duration displayed by some players to be inaccurate, usually greater than the actual duration of the video. Based on this, an embodiment of the present application provides a video cropping method, wherein when obtaining a key frame of a video to be cropped, if the first frame is not an I frame, the video to be cropped is traversed and the first I frame is used as the key frame. Figure 2 As shown in the figure, the first frame of the video to be cropped is a P frame (P_1), then the video to be cropped is traversed, and the first I frame is used as the key frame. Figure 2 The first I frame is recorded as I_1, that is, the key frame corresponding to the video to be cropped is I_1 shown in the figure.
[0052] In some embodiments, if the first frame is an I frame, the first frame is used as the key frame, or the video to be cropped is traversed and the second I frame is used as the key frame. It is understandable that when cropping a video, in some cases, the first frame of the video to be cropped may be an I frame. In this case, the first frame may be used as the key frame of the video to be cropped, or the video to be cropped may be traversed and the second I frame may be used as the key frame.
[0053] Then, the video to be cropped is divided into a first cropping segment and a second cropping segment based on the key frame, so that the key frame becomes the first frame of the second cropping segment. According to the above key frame determination step, there are two cases:
[0054] One is that if the first frame of the video to be cropped is not a key frame, then dividing the video to be cropped into a first cropped segment and a second cropped segment based on the key frame includes: determining the first cropped segment from the first frame of the video to be cropped to the key frame; and determining the second cropped segment from the key frame to the last frame of the video to be cropped. Figure 2 The example below illustrates Figure 2 As shown, the first frame of the video to be cropped is not a key frame, then the video to be cropped is divided into cropped segment A and cropped segment B according to the key frame I_1, wherein the first frame of the cropped segment B is the key frame I_1, and the subsequent steps S12-S14 are performed accordingly.
[0055] Secondly, if the first frame of the video to be cropped is a key frame, then dividing the video to be cropped into the first cropped segment and the second cropped segment based on the key frame further includes: considering the video to be cropped as the second cropped segment. In this case, the first cropped segment contains 0 video frames, the entire video to be cropped is considered as the second cropped segment, the first frame of the second cropped segment is a key frame, and only steps S13 and S14 need to be performed subsequently.
[0056] S12: re-encode the first cropped segment to obtain a first video. Figure 2 An example is used to illustrate, that is, the cropped segment A (P_1 to I_1) is re-encoded. It can be understood that the video length of the cropped segment A will not exceed the length of a GOP (all video frames between two adjacent I frames), and usually the GOP time will not be very long, most of the time less than 10 seconds, so the time spent on re-encoding the video segment will also be very short, usually within one second. In addition, the first frame of the first video after re-encoding must be an I frame, so the PTS of the first frame (I frame) will be 0, so the display length of the first video on all players will be consistent with its actual video length.
[0057] S13: Copy the second cropped segment to obtain a second video. Figure 2 The example of is used to illustrate, that is, directly copying video segment B (I_1 to P_2), the duration of this video segment is different in different application scenarios, it may be a few seconds or a few hours. Since the first frame of the second video is an I frame, that is, the PTS of the I frame is 0, the display duration of the second video is also equal to the actual duration of its video. In addition, compared with video re-encoding, the time consumption of directly copying the video is very short, and the longer the video duration, the higher the video clarity, the greater the difference in time consumption between directly copying the video and re-encoding the video. Therefore, copying the second cropped segment to obtain the second video can save a lot of time and is highly efficient.
[0058] S14, splicing the first video and the second video to obtain a cropped video. Specifically, splicing the first video and the second video according to the key frame; adjusting the timestamp of the cropped video. Figure 2 The example of splicing video segments A and B together according to key frame I_1 is also very short. Assume that the cropped video is recorded as video C. Since the first frame of video C is the first frame of video segment A, that is, I frame, and its PTS is 0, the display duration of video C is also accurate, that is, in any player, the display duration of video C is the same as its actual video duration.
[0059] A video cropping method provided by an embodiment of the present application obtains a key frame of a video to be cropped, and divides the video to be cropped into a first cropped segment and a second cropped segment based on the key frame, so that the key frame becomes the first frame of the second cropped segment, and then re-encodes the first cropped segment to obtain a first video, copies the second cropped segment to obtain a second video, and then splices the first video and the second video to obtain a cropped video. The part that needs to be re-encoded in this process is very short, the entire method is very short, and the efficiency of video cropping is very high. In addition, the first frame of the video cropped by this method must be an I frame, so it has good compatibility with all players, the display duration in the player is accurate, and the player end does not need to calculate whether the first frame to be decoded is a B frame or a P frame, thereby avoiding performance differences between different players.
[0060] See also Figure 3 , the embodiment of the present application provides a video cropping device, such as Figure 3 As shown, the video cropping device 200 includes a video segmentation module 21 , a first processing module 22 , a second processing module 23 and a video splicing module 24 .
[0061] Specifically, the video segmentation module 21 can obtain the key frame of the video to be cropped, and divide the video to be cropped into a first cropped segment and a second cropped segment based on the key frame, so that the key frame becomes the first frame of the second cropped segment. The first processing module 22 can re-encode the first cropped segment to obtain a first video. The second processing module 23 can copy the second cropped segment to obtain a second video. The video splicing module 24 can splice the first video and the second video to obtain a cropped video.
[0062] The video segmentation module 21 may specifically further: determine the start time of the video to be cropped, and determine the first frame of the video to be cropped according to the start time; if the first frame is not an I frame, traverse the video to be cropped and use the first I frame as the key frame; if the first frame is an I frame, use the first frame as the key frame, or traverse the video to be cropped and use the second I frame as the key frame.
[0063] The embodiment of the present application provides a video cropping device, which includes a video segmentation module, a first processing module, a second processing module and a video splicing module. The device can obtain the key frame of the video to be cropped, and divide the video to be cropped into a first cropped segment and a second cropped segment based on the key frame, so that the key frame becomes the first frame of the second cropped segment, and then re-encodes the first cropped segment to obtain the first video, copies the second cropped segment to obtain the second video, and then splices the first video and the second video to obtain the cropped video. The part that needs to be re-encoded in this process is very short, the whole method is very short, and the efficiency of video cropping is very high. In addition, the first frame of the video cropped by this method must be an I frame, so it has good compatibility with all players, the display duration in the player is accurate, and the player end does not need to calculate the decoding of the first frame is a B frame or a P frame, avoiding performance differences between different players.
[0064] It should be noted that the above video cropping device can execute the video cropping method provided in the embodiment of the present application, and has the corresponding functional modules and beneficial effects of the execution method. For technical details not fully described in the embodiment of the video cropping device, please refer to the video cropping method provided in the embodiment of the present application.
[0065] See also Figure 4 , Figure 4 3 is a schematic diagram of the hardware structure of an electronic device 30 provided in an embodiment of the present invention. The electronic device 30 includes one or more processors 31 and a memory 32. Figure 4 A processor 31 is taken as an example. The processor 31 and the memory 32 may be connected via a bus or other means. Figure 4 The example of connecting through bus is taken in the following.
[0066] The memory 31 is a non-volatile computer-readable storage medium that can be used to store non-volatile software programs, non-volatile computer executable programs and modules, such as program instructions / modules corresponding to the file storage method and the video cropping method in the embodiments of the present invention (for example, Figure 3 The processor 31 executes various functional applications and data processing of the electronic device 30 by running the non-volatile software program, non-volatile computer executable program and module stored in the memory 32, that is, the file storage method and video cropping method in the above method embodiment are implemented.
[0067] The memory 32 may include a program storage area and a data storage area, wherein the program storage area may store an operating system and an application required by at least one function; the data storage area may store data created according to the use of the file storage device and the video cutting device, etc. In addition, the memory 32 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some embodiments, the memory 32 may optionally include a memory remotely arranged relative to the processor 31, and these remotely arranged memories may be connected to the file storage device and the video cutting device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0068] The one or more modules are stored in the memory 32, and when executed by the one or more processors 31, the file storage method and the video cropping method in the above method embodiment are executed, for example: Figure 1 The method shown.
[0069] The above product can execute the file storage method and video cropping method provided in the embodiment of the present invention, and has the corresponding functional modules and beneficial effects of executing the file storage method and video cropping method. For technical details not described in detail in this embodiment, please refer to the file storage method and video cropping method provided in the embodiment of the present invention.
[0070] The electronic device 30 of the embodiment of the present invention may exist in various forms, including but not limited to servers, server clusters, cloud servers and other electronic devices with data interaction functions.
[0071] An embodiment of the present invention further provides a non-volatile computer storage medium, wherein the non-volatile computer storage medium stores computer executable instructions, and the computer executable instructions are executed by one or more processors, such as Figure 4 A processor 31 in the embodiment may enable the one or more processors to execute the file storage method and the video cropping method in any of the above method embodiments.
[0072] An embodiment of the present invention also provides a computer program product, which includes a computer program stored on a non-volatile computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by the electronic device, the electronic device executes the file storage method and video cropping method in the above embodiments.
[0073] The above described device or equipment embodiments are merely illustrative, wherein the unit modules described as separate components may or may not be physically separated, and the components displayed as module units may or may not be physical units, that is, they may be located in one place, or may be distributed on multiple network module units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present embodiment.
[0074] Through the description of the above implementation methods, ordinary technicians in this field can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, it can also be implemented by hardware. Ordinary technicians in this field can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing related hardware through a computer program, and the program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.
[0075] 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. Under the concept of the present application, the technical features in the above embodiments or different embodiments can also be combined, the steps can be implemented in any order, and there are many other changes in different aspects of the present application as described above, which are not provided in detail for the sake of simplicity. Although the present application has been described in detail with reference to the aforementioned embodiments, a person of ordinary skill in the art should understand that the technical solutions described in the aforementioned embodiments can still be modified, or some of the technical features can be replaced by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A video cropping method, characterized in that: The method comprises: Acquire a key frame of the video to be cropped, and divide the video to be cropped into a first cropping segment and a second cropping segment based on the key frame, so that the key frame becomes the first frame of the second cropping segment; Re-encoding the first cropped segment to obtain a first video; Copying the second cropped segment to obtain a second video; The first video and the second video are spliced to obtain a cropped video.
2. The method according to claim 1, characterized in that The step of obtaining the key frame of the video to be cropped includes: Determine the start time of the video to be cropped, and determine the first frame of the video to be cropped according to the start time; If the first frame is not an I frame, traverse the video to be cropped and take the first I frame as the key frame; If the first frame is an I frame, the first frame is used as the key frame, or the video to be cropped is traversed and the second I frame is used as the key frame.
3. The method according to claim 1, characterized in that: If the first frame of the video to be cropped is not a key frame, dividing the video to be cropped into a first cropped segment and a second cropped segment based on the key frame includes: Determine the first frame to the key frame of the video to be cropped as the first cropped segment; Determine the second cropped segment from the key frame to the last frame of the video to be cropped.
4. The method according to claim 3, characterized in that The re-encoding the first cropped segment further comprises: Get the most recent I frame before the key frame; The first cropped segment is re-encoded in conjunction with the most recent I frame.
5. The method according to claim 1, characterized in that If the first frame of the video to be cropped is a key frame, dividing the video to be cropped into a first cropped segment and a second cropped segment based on the key frame further includes: The video to be cropped is regarded as the second cropped segment.
6. The method according to claim 1, characterized in that The step of stitching the first video and the second video to obtain a cropped video includes: splicing the first video and the second video according to the key frames; Adjust the timestamps of the cropped video.
7. A video cutting device, characterized in that: include: A video segmentation module, configured to obtain a key frame of the video to be cropped, and divide the video to be cropped into a first cropped segment and a second cropped segment based on the key frame, so that the key frame becomes a first frame of the second cropped segment; A first processing module, configured to re-encode the first cropped segment to obtain a first video; A second processing module, used for copying the second cropped segment to obtain a second video; The video stitching module is used to stitch the first video and the second video to obtain a cropped video.
8. The video cutting device according to claim 7, characterized in that: The video segmentation module is also used for: Determine the start time of the video to be cropped, and determine the first frame of the video to be cropped according to the start time; If the first frame is not an I frame, traverse the video to be cropped and take the first I frame as the key frame; If the first frame is an I frame, the first frame is used as the key frame, or the video to be cropped is traversed and the second I frame is used as the key frame.
9. An electronic device, characterized in that: include: at least one processor; a memory communicatively coupled to the at least one processor; The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the video cropping method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Video file editing method and device
CN103024394A
Streaming media file accurate editing method and device
CN111918121A
Method and apparatus for editing video files
US6201925B1