A video file subtitle parsing and display method
By detecting and initializing the internal subtitles and external subtitles in the video file, and setting the subtitle attributes, smooth switching and improved compatibility between different subtitle formats in the Android system video player are achieved, which solves the problem of insufficient subtitle support in the existing technology. and switching failure issues.
Patent Information
- Application Number
- CN202110279829.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-16
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2041-03-16
AI Technical Summary
The existing video player software for Android systems does not fully support subtitle formats, and display problems often occur when switching subtitles, resulting in switching failures. It is difficult to achieve smooth switching and compatibility improvement between subtitles in different formats.
By detecting the internal subtitles of the video file and the external subtitles in srt, idx/sub, and microdvd formats, the subtitle data is initialized and the subtitle attributes are set. The player switches and displays subtitle types based on these attributes, and uses subtitle switching tags to achieve different Flexible switching between subtitle formats.
It greatly improves the compatibility of subtitle display and the smooth switching between subtitles in different formats, supports smooth switching between subtitles in different formats, and solves the problem of subtitle display compatibility and switching.
Smart Images

Figure CN113141540B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method for parsing and displaying subtitles of a video file. Background Art
[0002] With the rapid development of the Internet, video resources have exploded. Today, the Internet can watch video resources from all over the world. The languages used in video resources from different countries are different. In order to see the content of the video resources, it is particularly important to display subtitles when the video resources are played, which is directly related to whether the video resources can be enjoyed normally.
[0003] Mainstream subtitles include internal subtitles, srt format external subtitles, idx / sub format external subtitles, microdvd format external subtitles, etc. Although various Android-based video player software on the market support subtitle functions, the support is not comprehensive enough, only supporting a few or one type of subtitles, and there will be some display problems when switching subtitles, resulting in switching failure.
[0004] Therefore, how to provide a video file subtitle parsing and display method to improve the compatibility of subtitle display and support smooth switching between subtitles of different formats has become an urgent problem to be solved. Summary of the invention
[0005] The technical problem to be solved by the present invention is to provide a method for parsing and displaying subtitles of a video file, thereby improving the compatibility of subtitle display and supporting smooth switching between subtitles of different formats.
[0006] The present invention is implemented as follows: a method for parsing and displaying subtitles of a video file, comprising the following steps:
[0007] Step S10: The terminal receives the video file to be played, and enables the subtitle display function based on the received subtitle activation instruction;
[0008] Step S20, detecting internal subtitles of the video file and external subtitles of different formats, initializing each of the internal subtitles and external subtitles to obtain subtitle data, and setting subtitle attributes of the subtitle data;
[0009] Step S30, sending each of the subtitle data to a player of the terminal;
[0010] Step S40: The player switches and displays the subtitle type of the subtitle data based on the subtitle attribute.
[0011] Furthermore, the step S20 specifically includes:
[0012] Step S21, detecting the internal subtitles carried in the video file through the subtitle stream of the video file, and detecting the external subtitles in the same path through the file name of the video file; the format of the external subtitles is srt, idx / sub or microdvd;
[0013] Step S22, parse the internal subtitles, external subtitles in srt format, external subtitles in idx / sub format, and external subtitles in microdvd format to obtain subtitle data, set subtitle attributes for each of the subtitle data, and store the subtitle data carrying the subtitle attributes in a subtitle queue.
[0014] Furthermore, in step S22, parsing the internal subtitles to obtain subtitle data specifically includes:
[0015] Step S2211, reading the subtitle data packet carried by the internal subtitles through the read_stream thread;
[0016] Step S2212: Decode the subtitle data packet through FFMPEG to obtain subtitle data.
[0017] Furthermore, in step S22, parsing the external subtitles in the srt format to obtain subtitle data specifically includes:
[0018] Step S2221, open the external subtitles in srt format according to the file name;
[0019] Step S2222: parse the text content of the external subtitles in the srt format line by line to obtain subtitle data.
[0020] Furthermore, in step S22, the parsing of the external subtitles in the idx / sub format to obtain subtitle data specifically includes:
[0021] Step S2231, open the idx file and sub file according to the file name;
[0022] Step S2232, parsing the idx file to obtain the time information corresponding to each text content and the position in the sub file;
[0023] Step S2233: read the corresponding data from the sub file based on the position, and save it as an RGBA array, that is, subtitle data.
[0024] Further, in step S22, the parsing of the external subtitles in the microdvd format to obtain subtitle data specifically includes:
[0025] Step S2241, opening the external subtitles in microdvd format according to the file name;
[0026] Step S2242, parsing the text content of the external subtitles in the microDVD format line by line to obtain subtitle data.
[0027] Furthermore, in step S20, the subtitle attributes include the total number of subtitle characters, subtitle switching labels, display time corresponding to each line of subtitles, subtitle sequence numbers corresponding to each line of subtitles, and subtitle types;
[0028] The subtitle switching label is internal subtitle switching to external subtitle, external subtitle switching to internal subtitle, maintaining internal subtitle or maintaining external subtitle; the subtitle type is internal subtitle or external subtitle.
[0029] Furthermore, the step S30 is specifically as follows:
[0030] Each of the subtitle data stored in the subtitle queue is sent to the player of the terminal through the socket based on the display time.
[0031] Furthermore, the step S40 specifically includes:
[0032] Step S41, the player sorts each line of subtitle data based on the display time and subtitle sequence number in the subtitle attributes;
[0033] Step S42, the player reads the subtitle switching tag in the subtitle attributes, and if the subtitles are switched from internal to external, the subtitle data of the subtitle type of internal subtitles is released, and the subtitle data of the subtitle type of external subtitles is displayed; if the subtitles are switched from external to internal, the subtitle data of the subtitle type of internal subtitles is initialized and displayed; if the subtitles are maintained, the subtitle data of the subtitle type of external subtitles is released, and the subtitle data of the subtitle type of internal subtitles is displayed; if the subtitles are maintained, the subtitle data of the subtitle type of external subtitles is directly displayed.
[0034] The advantages of the present invention are:
[0035] By detecting the internal subtitles of the video file and the external subtitles in the srt, idx / sub, and microdvd formats, each internal subtitle and external subtitle is initialized to obtain subtitle data, and each subtitle data including the total number of subtitle words, the subtitle switching label, the display time corresponding to each line of subtitles, the subtitle sequence number corresponding to each line of subtitles, and the subtitle attributes of the subtitle type are set. The player can switch and display the subtitle type of the received subtitle data based on the subtitle attributes; that is, flexible switching display between internal subtitles and external subtitles in the srt, idx / sub, and microdvd formats is realized. Since the switching attributes defined by the subtitle switching label are realized, the smoothness of switching between subtitles in different formats is greatly improved, and finally the compatibility of subtitle display is greatly improved, and smooth switching between subtitles in different formats is supported. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The present invention will be further described below in conjunction with embodiments with reference to the accompanying drawings.
[0037] Figure 1 The present invention is a flowchart of a method for parsing and displaying subtitles of a video file. DETAILED DESCRIPTION
[0038] The technical solution in the embodiments of the present application has the following overall idea: simultaneously detecting and initializing the internal subtitles of the video file and the external subtitles in the srt, idx / sub, and microdvd formats, and performing subtitle type switching and display of the received subtitle data based on the subtitle attributes, so as to improve the compatibility of the subtitle display and support smooth switching between subtitles in different formats.
[0039] Please refer to Figure 1 As shown, a preferred embodiment of a method for parsing and displaying subtitles of a video file of the present invention comprises the following steps:
[0040] Step S10: The terminal receives the video file to be played, and enables the subtitle display function based on the received subtitle activation instruction;
[0041] Step S20, detecting internal subtitles of the video file and external subtitles of different formats, initializing each of the internal subtitles and external subtitles to obtain subtitle data, and setting subtitle attributes of the subtitle data; the internal subtitles are subtitles that come with the video file, and the external subtitles need to be separately loaded into the player for display;
[0042] Step S30, sending each of the subtitle data stored in the subtitle queue to the player of the terminal through the socket based on the display time; before sending, the socket connection needs to be initialized;
[0043] Step S40: The player switches and displays the subtitle data in real time based on the subtitle attributes, for example, switching from the internal subtitles to external subtitles in srt format, or switching from the external subtitles in srt format to external subtitles in microdvd format.
[0044] The step S20 specifically includes:
[0045] Step S21, detecting the internal subtitles carried in the video file through the subtitle stream of the video file, and detecting the external subtitles in the same path through the file name of the video file; the format of the external subtitles is srt, idx / sub or microdvd; the external subtitles have the same file name as the video file;
[0046] If the internal subtitles are detected, the subtitle stream of the first internal subtitle is displayed by default; if the internal subtitles are not detected, the related process of the internal subtitles is skipped;
[0047] Step S22, parse the internal subtitles, external subtitles in srt format, external subtitles in idx / sub format, and external subtitles in microdvd format to obtain subtitle data, set subtitle attributes for each of the subtitle data, and store the subtitle data carrying the subtitle attributes in a subtitle queue.
[0048] In the step S22, the parsing of the internal subtitles to obtain subtitle data specifically includes:
[0049] Step S2211, reading the subtitle data packet carried by the internal subtitles through the read_stream thread;
[0050] Step S2212: Decode the subtitle data packet through FFMPEG to obtain subtitle data.
[0051] In the step S22, the step of parsing the external subtitles in the srt format to obtain subtitle data specifically includes:
[0052] Step S2221, open the external subtitles in srt format according to the file name;
[0053] Step S2222: parse the text content of the external subtitles in the srt format line by line to obtain subtitle data.
[0054] In the step S22, the step of parsing the external subtitles in the idx / sub format to obtain subtitle data specifically includes:
[0055] Step S2231, open the idx file and sub file according to the file name;
[0056] Step S2232, parsing the idx file to obtain the time information corresponding to each text content and the position in the sub file;
[0057] Step S2233: read the corresponding data from the sub file based on the position, and save it as an RGBA array, that is, subtitle data.
[0058] In the step S22, the step of parsing the external subtitles in the microdvd format to obtain subtitle data specifically includes:
[0059] Step S2241, opening the external subtitles in microdvd format according to the file name;
[0060] Step S2242, parsing the text content of the external subtitles in the microDVD format line by line to obtain subtitle data.
[0061] In the step S20, the subtitle attributes include the total number of subtitle characters, subtitle switching labels, display time corresponding to each line of subtitles, subtitle sequence numbers corresponding to each line of subtitles, and subtitle types;
[0062] The subtitle switching label is internal subtitle switching to external subtitle, external subtitle switching to internal subtitle, maintaining internal subtitle or maintaining external subtitle; the subtitle type is internal subtitle or external subtitle.
[0063] The step S40 specifically includes:
[0064] Step S41, the player sorts each line of subtitle data based on the display time and subtitle sequence number in the subtitle attributes;
[0065] Step S42, the player reads the subtitle switching tag in the subtitle attributes, and if the subtitles are switched from internal to external, the subtitle data of the subtitle type is released, and the subtitle data of the subtitle type is displayed in real time; if the subtitles are switched from external to internal, the subtitle data of the subtitle type is initialized and displayed in real time; if the subtitles are maintained, the subtitle data of the subtitle type is released, and the subtitle data of the subtitle type is displayed in real time; if the subtitles are maintained, the subtitle data of the subtitle type is directly displayed.
[0066] In summary, the advantages of the present invention are:
[0067] By detecting the internal subtitles of the video file and the external subtitles in the srt, idx / sub, and microdvd formats, each internal subtitle and external subtitle is initialized to obtain subtitle data, and each subtitle data including the total number of subtitle words, the subtitle switching label, the display time corresponding to each line of subtitles, the subtitle sequence number corresponding to each line of subtitles, and the subtitle attributes of the subtitle type are set. The player can switch and display the subtitle type of the received subtitle data based on the subtitle attributes; that is, flexible switching display between internal subtitles and external subtitles in the srt, idx / sub, and microdvd formats is realized. Since the switching attributes defined by the subtitle switching label are realized, the smoothness of switching between subtitles in different formats is greatly improved, and finally the compatibility of subtitle display is greatly improved, and smooth switching between subtitles in different formats is supported.
[0068] Although the specific implementation modes of the present invention are described above, those skilled in the art should understand that the specific implementation modes described are only illustrative and are not intended to limit the scope of the present invention. Equivalent modifications and changes made by those skilled in the art in accordance with the spirit of the present invention should be included in the scope of protection of the claims of the present invention.
Claims
1. A method for parsing and displaying subtitles of a video file, characterized in that: The steps include: Step S10: The terminal receives the video file to be played, and enables the subtitle display function based on the received subtitle activation instruction; Step S20, detecting internal subtitles of the video file and external subtitles of different formats, initializing each of the internal subtitles and external subtitles to obtain subtitle data, and setting subtitle attributes of the subtitle data; Step S30, sending each of the subtitle data stored in the subtitle queue to the player of the terminal through the socket based on the display time; Step S40: the player switches and displays the subtitle type of the subtitle data based on the subtitle attribute; The step S20 specifically includes: Step S21, detecting the internal subtitles carried in the video file through the subtitle stream of the video file, and detecting the external subtitles in the same path through the file name of the video file; the format of the external subtitles is srt, idx / sub or microdvd; Step S22, parsing the internal subtitles, external subtitles in srt format, external subtitles in idx / sub format, and external subtitles in microdvd format to obtain subtitle data, setting subtitle attributes of each subtitle data, and storing the subtitle data carrying the subtitle attributes in a subtitle queue; The subtitle attributes include the total number of subtitle words, subtitle switching label, display time corresponding to each line of subtitles, subtitle sequence number corresponding to each line of subtitles, and subtitle type; The subtitle switching label is internal subtitles switching to external subtitles, external subtitles switching to internal subtitles, maintaining internal subtitles or maintaining external subtitles; the subtitle type is internal subtitles or external subtitles; The parsing of the internal subtitles to obtain subtitle data specifically includes: Step S2211, reading the subtitle data packet carried by the internal subtitles through the read_stream thread; Step S2212, decoding the subtitle data packet through FFMPEG to obtain subtitle data; The parsing of the external subtitles in the srt format to obtain subtitle data specifically includes: Step S2221, open the external subtitles in srt format according to the file name; Step S2222, parsing the text content of the external subtitles in the srt format line by line to obtain subtitle data; In the step S22, the step of parsing the external subtitles in the idx / sub format to obtain subtitle data specifically includes: Step S2231, open the idx file and sub file according to the file name; Step S2232, parsing the idx file to obtain the time information corresponding to each text content and the position in the sub file; Step S2233: read the corresponding data from the sub file based on the position, and save it as an RGBA array, that is, subtitle data.
2. A video file subtitle parsing and displaying method as claimed in claim 1, characterized in that: In the step S22, the step of parsing the external subtitles in the microdvd format to obtain subtitle data specifically includes: Step S2241, opening the external subtitles in microdvd format according to the file name; Step S2242, parsing the text content of the external subtitles in the microDVD format line by line to obtain subtitle data.
3. A video file subtitle parsing and displaying method as claimed in claim 1, characterized in that: The step S40 specifically includes: Step S41, the player sorts each line of subtitle data based on the display time and subtitle sequence number in the subtitle attributes; Step S42, the player reads the subtitle switching tag in the subtitle attributes, and if the subtitles are switched from internal to external, the subtitle data of the subtitle type of internal subtitles is released, and the subtitle data of the subtitle type of external subtitles is displayed; if the subtitles are switched from external to internal, the subtitle data of the subtitle type of internal subtitles is initialized and displayed; if the subtitles are maintained, the subtitle data of the subtitle type of external subtitles is released, and the subtitle data of the subtitle type of internal subtitles is displayed; if the subtitles are maintained, the subtitle data of the subtitle type of external subtitles is directly displayed.
