Time shift implementation method based on browser-side playing mpegtsTechnical Field
The invention relates to the technical field of audio and video players, in particular to a time shifting function implementation method based on browser-side playing mpegts.
Background
Mpegts video formats are widely used in the fields of broadcast television and IPTV, and are intended to realize playing at a browser end, a player of the player needs to be realized, mpegts video is played at a television end or a set top box, the realization of time shifting depends on the position of the video accurately positioned by a back-end server, or slices are set in advance, the information of each slice is stored, and then data of the beginning of an IDR frame at the time shifting position is returned to the set top box end or the television end for decoding and playing, so that the function of time shifting is completed.
In the actual application process, under the condition that a slice is not available or a back-end server is positioned accurately, a pure browser end plays and finishes a time shifting function to analyze the positions of all IDR frames in a video file and the positions of time shifting time points in the video, and all frames can be positioned only by reading the whole video file, but the browser end plays and needs to be based on a network, and because the browser limits, the file is stored in a memory, if the video file is too large, the excessive memory is occupied, even memory overflows, and if the network is slower, the downloading time is long, so that the realization of playing mpegts video based on the browser end is not available at present.
Disclosure of Invention
The invention aims to provide a time shift implementation method based on browser-side playing mpegts, which aims to solve the problems that in the prior art, network-based playing is needed for the browser-side playing, if a video file is large, the memory occupation of the browser is large and the time for waiting to download the file is long, so that the realization of a plurality of video playing mpegts based on the browser-side is not provided with a time shift function.
In order to achieve the above purpose, the invention provides a time shift implementation method based on browser-side playing mpegts, which comprises the following steps:
S1, initializing and acquiring the size of a video file by a player;
s2, initializing and acquiring a maximum display time maxpts and a minimum display time minpts by the player;
s3, acquiring the percentage p of the time point of the time shift position in the total duration;
S4, calculating the range of the video file to be loaded according to the percentage and the size of each loading packet, and dynamically adjusting the size of each loading video file according to the network condition and the use condition of the memory so as to adapt to the current bandwidth condition;
S5, extracting all the display time pts ranges of the video clips in the step S4, and caching the loaded video clips for users to directly read data from the caches when fast forward and fast backward operations are performed;
s6, calculating pts corresponding to the time shift point, and judging whether the loaded video file contains the time point of time shift or not;
S7, judging whether the pts in the step S6 are in the range of the pts extracted in the step S5, if so, indicating that the time-shifted frame is in the currently loaded video range, namely ranPts is larger than rMinPts and smaller than rMaxPts, namely hit, and after hit, searching the latest IDR frame forwards, and decoding from the frame;
S8, if no hit exists, continuously calculating the starting position and the ending position of the next segment of data according to the data, and determining whether the video clip needs to be loaded in advance according to the network condition and the cache condition;
and S9, if not found, repeating the step S8 until found based on the starting position and the ending position in the step S8.
Preferably, in step S1, the initialization procedure of the player is to initialize an http request to obtain the size of the video file.
Preferably, in step S2, the maximum display time is obtained by extracting the last (or multiple consecutive) video segments of a suitable size, and the minimum display time is obtained by extracting the first (or multiple consecutive) video segments, typically taking a gp as a criterion.
Preferably, in step S3, the percentage flow of the time point of the time-shift position in the total duration is that the total duration of the video is first obtained according to the maximum display time and the minimum display time, then the current time point of the time-shift position is obtained, and finally the percentage of the time point of the time-shift position in the total duration is obtained through calculation.
Preferably, in step S4, in the calculating the range of loading the video file, an integer multiple of the packet length is taken up and down during the calculation, including the following steps:
s41, presetting a single loading size psize according to the total size and the total duration of the video;
S42, calculating the initial position of the loading file, wherein the initial position is the position of the percentage P in the S3 in the total size of the video, and taking down half of the single loading size psize;
s43, loading psize video clips according to the initial position;
s44, using a cache to store the downloaded video clips for fast forward and fast backward operations of a user, so that the data can be directly read from the cache when the user performs the fast forward and fast backward operations, and the request times of the server are reduced;
S45, managing video clips in the cache by using an LRU cache algorithm so as to improve the hit rate of the cache.
Preferably, in step S5, the display time pts of the extracted video is rMinPts-rMaxPts.
Preferably, in step S6, the pts corresponding to the calculated time shift point is a time shift point percentage P.
Preferably, in step S8, the continuing to calculate the position where the next piece of data starts and the position where the next piece of data ends includes the following steps:
s81, if ranPts is larger than rMaxPts, searching up the ending position of the current fragment;
S82, if ranPts is smaller than rMaxPts, searching downwards with the starting position of the current fragment;
s83, and then repeating step S5.
Preferably, in step S8, the process of determining whether to load video clips in advance according to the network condition and the buffering condition is that, when the network condition is good, more video clips can be loaded in advance to provide faster response speed and better user experience, and when the network condition is poor, the size of the loaded video clips can be dynamically adjusted according to the real-time bandwidth condition to avoid network congestion and video playing jam.
Preferably, in step S9, the repeating step S8 includes the steps of:
S91, judging whether the pts of the extracted video in the step S8 is within the range of the pts extracted in the step S5, namely, the time-shifted frame is within the range of the currently loaded video;
S92, if the pts of the extracted video in the step S8 is not in the range, repeating the step S8 based on the start and end of the step S8, and calculating the starting position and ending position of the next piece of data again;
S93, if the pts of the extracted video in the step S8 is within the range, finding the pts of the video file and the corresponding file position.
Compared with the prior art, the invention has the beneficial effects that:
1. According to the method, the percentage of the time point of the time shift position in the total duration is obtained by recording the starting position and the display time pts of all frames which are downloaded and analyzed in the video file, then the range of loading the video file is calculated according to the percentage and the size of each loading packet, a section of video file is loaded from the position, and the pts and the corresponding file positions of the section of video file are analyzed, so that the quick initialization of the video of the browser player mpegts is realized, and the quick time shift function is realized under the condition that an excessive memory is not occupied.
2. In the method, the downloaded video clips are stored by using the cache, so that data can be directly read from the cache when a user performs fast forward and fast backward operations, the request times to a server are reduced, the hit rate of the cache is improved by managing the video clips in the cache, more video clips can be loaded in advance when the network condition is good, so as to provide faster response speed and better user experience, and the size of the loaded video clips can be dynamically adjusted according to the real-time bandwidth condition when the network condition is poor, so as to avoid network congestion and video playing clamping
Drawings
Fig. 1 is a flowchart of a time shift implementation method based on browser-side play mpegts according to the present invention.
Detailed Description
The following description of the embodiments of the present invention will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present invention, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
Referring to FIG. 1, the invention provides a method for realizing time shift based on browser-side playing mpegts, which comprises the following steps:
step one, initializing and obtaining the size of a video file by a player; initializing an http request to obtain the size of a video file;
Initializing and acquiring maximum display time maxpts and minimum display time minpts by the player, acquiring the maximum display time according to the total duration of displaying the video file on the interface, and acquiring the minimum display time by dragging a progress bar or checking time display;
The maximum display time is obtained by extracting the last (or multiple continuous) video segments with proper size, and the minimum display time is obtained by extracting the first (or multiple continuous) video segments, which is generally based on the acquisition of a gp.
Acquiring the percentage p of the time point of the time shift position in the total duration, wherein the percentage flow of the time point of the time shift position in the total duration is that firstly, the total duration of the video is acquired according to the maximum display time and the minimum display time, then, the current time point of the time shift position is acquired, and finally, the percentage of the time point of the time shift position in the total duration is obtained through calculation;
Calculating the range of loading video files according to the percentage and the size of each loading packet, dynamically adjusting the size of each loading video file according to the network condition and the use condition of a memory so as to adapt to the current bandwidth condition, avoiding network congestion and too slow video loading, loading a section of video file from the position, and analyzing pts and the corresponding file position of the section of video file;
Specifically, in the range of the video file to be loaded, the integer multiple of the packet length is taken upwards and downwards during calculation, and the method comprises the following steps:
41 Presetting a single loading size psize according to the total size and the total duration of the video;
42 Calculating the initial position of the loading file, wherein the initial position is the position of the percentage P in the S3 in the total size of the video, and taking down half of the single loading size psize;
43 Loading psize-sized video clips according to the starting position;
44 Using a cache to store the downloaded video clips for fast forward and fast reverse operations by a user;
45 Using LRU caching algorithm to manage video clips in the cache;
Step five, extracting the display time pts range of the video clips in the step four, and caching the loaded video clips, wherein the display time pts range of the extracted video is rMinPts-rMaxPts;
step six, calculating pts corresponding to the time shift point, which is used for judging whether the loaded video file contains the time point of time shift, wherein the pts corresponding to the time shift point is calculated as the percentage P of the total duration of the video;
Judging whether the pts in the step six is in the range of the pts extracted in the step five, if so, indicating that the time-shifted frame is in the currently loaded video range, namely ranPts is larger than rMinPts and smaller than rMaxPts, namely hitting a point for finding the time shift;
step eight, if no hit exists, continuously calculating the starting position and the ending position of the next section of data according to the data, and determining whether the video clip needs to be loaded in advance according to the network condition and the cache condition so as to improve the user experience;
specifically, the continuing calculation of the position at which the next piece of data starts and the position at which the next piece of data ends includes the steps of:
81 If ranPts is greater than rMaxPts, the end position of the current segment is looked up upwards;
82 If ranPts is less than rMaxPts, looking down with the starting position of the current segment;
83 Step S5) is then repeated.
The method comprises the steps of determining whether to load video clips in advance according to network conditions and cache conditions, wherein when the network conditions are good, more video clips can be loaded in advance to provide faster response speed and better user experience;
step nine, if not found, repeating the step eight until found based on the starting position and the ending position in the step eight;
specifically, the repeating step eight includes the steps of:
91 Judging whether the pts of the extracted video in the step eight is in the range of the pts extracted in the step five, namely, the time-shifted frame is in the currently loaded video range;
92 If the pts of the extracted video in the step eight is not in the range, repeating the step eight based on the start and end of the step eight, and calculating the starting position and ending position of the next section of data again;
93 If the pts of the extracted video in the step eight is within the range, finding the pts of the video file and the corresponding file position;
According to the method, the starting position and the display time pts of all frames which are already downloaded and analyzed in a video file are recorded, then the approximate position of a time-shifted time point in the file is estimated according to recorded data, then a section of video file is loaded from the position, the pts of the section of video file and the corresponding file position are analyzed, if the pts and the corresponding file position are not found, the position of the beginning of the next section of file is repositioned according to the analyzed information of the section of video file until the beginning of the next section of video file is found, if the bandwidth is enough, for example, the access of an intranet is achieved, the first time of the estimated and positioned file point can find the time-shifted point, the second time can be positioned to the data point at most, the downloaded video fragment is stored by using a buffer memory, so that the data can be directly read from the buffer memory when a user performs fast forward and fast backward operations, the request times of a server are reduced, the video fragment in the buffer memory is managed, the hit rate of the buffer memory is improved, more video fragments can be loaded in advance when the network condition is good, the network condition is provided, the faster response speed and better user experience is provided, the user condition can be better, the video fragment can be played in advance, the condition of the video card can be adjusted in real time, the video condition can be adjusted, the video condition can be directly played by the video card can be adjusted according to the network condition, and the video condition can be adjusted in real time, and the video condition can be directly played by playing and the video condition is adjusted.
Although the present invention has been described with reference to the foregoing embodiments, it will be apparent to those skilled in the art that modifications may be made to the embodiments described, or equivalents may be substituted for elements thereof, and any modifications, equivalents, improvements and changes may be made without departing from the spirit and principles of the present invention.