Subtitle Tools in C Language
I decided to release some of the C language tools I've written over the years for working with subtitle files. These include SubRip (.srt) tools, a SubStationAlpha (.ssa)-to-SubRip (.srt) converter, as well as a PGS (.sup) tool to extract subtitle images as bitmaps, as well as synchronize the timestamps to new anchor points.
In addition, I have included routines developed for managing YCbCr-to-RGB, RGB-to-YCbCr, and derivation of BT.709 RGB colorspace constants from BT.709 color primaries.
Look at the top of each file to see infomation on compilation, inputs and outputs, etc.
Table 1: | SubRip (.srt) Tools |
check.c | Check for errors in a SubRip (.srt) file and report results. |
srt.c | Read an existing SubRip (.srt) file, apply positive, negative, or no offset to the time stamps, and save in an output file. Ignores existing subtitle numbers and renumbers them from 1 to N. I often use it just to renumber. |
ssa2srt.c | Read an existing SubStationAlpha (SSA) file and convert to SubRip (.srt) output file. Transfers styles and markups for font color, bold, italic, underline, strikeout, and alignment. Only recognizes V4 and V4+ Styles. Ignores those SSA style attributes and override tags not implemented in SubRip format. Warning: Unlike SubRip files, SSA files don't require subtitles to be in chronological order. The SubRip output file is not corrected for this rare situation here; use reorder.c below. |
ssa2srt-nostyles.c | Read an existing SubStationAlpha (SSA) file and convert to SubRip (.srt) output file. Doesn't transfer styles, only markups for font color, bold, italic, underline, strikeout, and alignment. Warning: Unlike SubRip files, SSA files don't require subtitles to be in chronological order. The SubRip output file is not corrected for this rare situation here; use reorder.c below. |
reorder.c | Re-order non-chronological subtitles in a SubRip (.srt) file by sorting on start times. |
Table 2: | PGS (.sup) Tool |
pgs.c | A tool to analyze a PGS (.sup) file and produce a report file. Optional functions include producing a bitmap file for each subtitle, and synchronizing all timestamps to new anchor-points. |
Table 3: | Chapter Tool |
chapters.c | Create an XML chapters file given the feature duration and desired number of chapters. The resulting .xml file can be added to a video container using tools like MKVToolNix GUI. There are often 12 to 16 chapters for a typical 1.5 hour feature. I usually use ffmpeg to find the duration. e.g., ffmpeg -i filename.mkv Note that chapters.c expects the same time notation as ffmpeg i.e., using fractions of a second. (This is different from SubRip/srt file format which is ",milliseconds".) You can copy and paste the result from ffmpeg. |
Table 4: | Colorspace Tools |
References: ITU-R BT.709-6, ITU-T H.273 (V4), and SMPTE RP 177-1993 |
bt709.c | Derive the RGB color constants for BT.709 YCbCr colorspace. The Normalized Primary Matrix (NPM), which includes KR, KG, and KB, is derived from the color primaries as defined in the BT.709 standard. |
ycbcr2rgb.c | Convert YCbCr (BT.709) to 8-bit sRGB. Assumes BT.709 color primaries and gamma-correction were used to produce YCbCr. Uses sRGB color primaries (same as BT.709) and applies sRGB gamma-correction to produce sRGB coordinates. |
rgb2ycbcr.c | Convert 8-bit sRGB to YCbCr (BT.709). Assumes sRGB gamma-correction was applied to sRGB. Uses BT.709 color primaries and applies BT.709 gamma-correction to produce YCbCr. |
P. David Buchan pdbuchan@gmail.com