Title: | Read and Manipulate Video Subtitles |
---|---|
Description: | A collection of functions to read, write and manipulate video subtitles. Supported formats include "srt", "subrip", "sub", "subviewer", "microdvd", "ssa", "ass", "substation", "vtt", and "webvtt". |
Authors: | Francois Keck [aut, cre], Bob Rudis [ctb] |
Maintainer: | Francois Keck <[email protected]> |
License: | GPL-3 |
Version: | 1.0 |
Built: | 2025-01-30 09:42:02 UTC |
Source: | https://github.com/fkeck/subtools |
subtitles
objectConvert an R object to a subtitles
object
as_subtitle(x, format = "auto", clean.tags = TRUE, metadata = data.frame(), frame.rate = NA, encoding = "auto", ...) ## Default S3 method: as_subtitle(x, format = "auto", clean.tags = TRUE, metadata = data.frame(), frame.rate = NA, encoding = "auto", ...) ## S3 method for class 'character' as_subtitle(x, format = "auto", clean.tags = TRUE, metadata = data.frame(), frame.rate = NA, encoding = "auto", ...)
as_subtitle(x, format = "auto", clean.tags = TRUE, metadata = data.frame(), frame.rate = NA, encoding = "auto", ...) ## Default S3 method: as_subtitle(x, format = "auto", clean.tags = TRUE, metadata = data.frame(), frame.rate = NA, encoding = "auto", ...) ## S3 method for class 'character' as_subtitle(x, format = "auto", clean.tags = TRUE, metadata = data.frame(), frame.rate = NA, encoding = "auto", ...)
x |
an R object that can be coerced into a |
format |
a character string specifying the format of the subtitles.
Four formats can be read: |
clean.tags |
logical. If |
metadata |
a one-row dataframe or tibble, or any object that can be coerced
into a one-row tibble by |
frame.rate |
a numeric value giving the frame rate in frames per second. Only relevant for MicroDVD format.
If |
encoding |
the name of the encoding to be used. Default is " |
... |
passed on to downstream methods. |
as_subtitle( c("WEBVTT", "X-TIMESTAMP-MAP=MPEGTS:181083,LOCAL:00:00:00.000", "", "3", "00:00:21.199 --> 00:00:22.333", ">> FEMALE SPEAKER:", "Don't stay up too late.", "", "" ), format = "webvtt" )
as_subtitle( c("WEBVTT", "X-TIMESTAMP-MAP=MPEGTS:181083,LOCAL:00:00:00.000", "", "3", "00:00:21.199 --> 00:00:22.333", ">> FEMALE SPEAKER:", "Don't stay up too late.", "", "" ), format = "webvtt" )
Bind subtitles
or/and multisubtitles
objects.
bind_subtitles(..., collapse = TRUE, sequential = TRUE)
bind_subtitles(..., collapse = TRUE, sequential = TRUE)
... |
|
collapse |
logical. If |
sequential |
logical. If |
A subtitles
object if collapse = TRUE
(default).
A multisubtitles
object if collapse = FALSE
.
f1 <- system.file("extdata", "ex_subrip.srt", package = "subtools") s1 <- read_subtitles(f1, metadata = tibble::tibble(Season = 1, Episode = 2)) f2 <- system.file("extdata", "ex_substation.ass", package = "subtools") s2 <- read_subtitles(f2, metadata = tibble::tibble(Season = 2)) bind_subtitles(s1, s2) bind_subtitles(s1, s2, collapse = FALSE)
f1 <- system.file("extdata", "ex_subrip.srt", package = "subtools") s1 <- read_subtitles(f1, metadata = tibble::tibble(Season = 1, Episode = 2)) f2 <- system.file("extdata", "ex_substation.ass", package = "subtools") s2 <- read_subtitles(f2, metadata = tibble::tibble(Season = 2)) bind_subtitles(s1, s2) bind_subtitles(s1, s2, collapse = FALSE)
Functions to clean subtitles. clean_tags
cleans formatting tags.
clean_captions
cleans close captions, i.e all text enclosed in parentheses or squared brackets.
clean_patterns
provides a more general and flexible cleaning based on regular expressions.
clean_tags(x, format = "srt", clean.empty = TRUE) clean_captions(x, clean.empty = TRUE) clean_patterns(x, pattern, clean.empty = TRUE)
clean_tags(x, format = "srt", clean.empty = TRUE) clean_captions(x, clean.empty = TRUE) clean_patterns(x, pattern, clean.empty = TRUE)
x |
a |
format |
the original format of the |
clean.empty |
logical. Should empty remaining lines ("") deleted after cleaning. |
pattern |
a character string containing a regular expression to be matched and cleaned. |
A subtitles
or multisubtitles
object.
This function uses mkvmerge
to extract tracks data from an MKV file.
You must have mkvmerge
installed on your computer.
get_mkv_info(file, mkvmerge.exec = "mkvmerge", print.info = TRUE)
get_mkv_info(file, mkvmerge.exec = "mkvmerge", print.info = TRUE)
file |
a character string giving the path to the MKV file. |
mkvmerge.exec |
a character string giving the path to the |
print.info |
print basic informations about subtitle tracks. Default is |
A list with complete data about the MKV is invisibly returned.
If the MKV has at least 1 subtitles track and print.info
is TRUE
, basic informations are printed.
Otherwise it returns a warning.
https://mkvtoolnix.download/downloads.html
This function extracts the raw text content of subtitles objects as a character string.
get_raw_text(x, collapse = " ")
get_raw_text(x, collapse = " ")
x |
an object of class |
collapse |
a character string to separate the subtitles lines. |
A character string.
f <- system.file("extdata", "ex_subrip.srt", package = "subtools") s <- read_subtitles(f) get_raw_text(s) cat(get_raw_text(s, collapse = "\n"))
f <- system.file("extdata", "ex_subrip.srt", package = "subtools") s <- read_subtitles(f) get_raw_text(s) cat(get_raw_text(s, collapse = "\n"))
Get basic informations for subtitle objects
get_subtitles_info(x)
get_subtitles_info(x)
x |
a |
s <- read_subtitles( system.file("extdata", "ex_subrip.srt", package = "subtools") ) get_subtitles_info(s)
s <- read_subtitles( system.file("extdata", "ex_subrip.srt", package = "subtools") ) get_subtitles_info(s)
Print method for multisubtitles
## S3 method for class 'multisubtitles' print(x, printlen = 10L, ...)
## S3 method for class 'multisubtitles' print(x, printlen = 10L, ...)
x |
a |
printlen |
the maximum number of subtitles to print. |
... |
further arguments passed to or from other methods. |
f <- system.file("extdata", "ex_subrip.srt", package = "subtools") s <- read_subtitles(f) bind_subtitles(s, s, collapse = FALSE)
f <- system.file("extdata", "ex_subrip.srt", package = "subtools") s <- read_subtitles(f) bind_subtitles(s, s, collapse = FALSE)
Reads subtitles from a file.
read_subtitles(file, format = "auto", clean.tags = TRUE, metadata = data.frame(), frame.rate = NA, encoding = "auto")
read_subtitles(file, format = "auto", clean.tags = TRUE, metadata = data.frame(), frame.rate = NA, encoding = "auto")
file |
the name of the file which the subtitles are to be read from. If it does not contain an absolute path, the file name is relative to the current working directory. |
format |
a character string specifying the format of the subtitles.
Four formats can be read: |
clean.tags |
logical. If |
metadata |
a one-row dataframe or tibble, or any object that can be coerced
into a one-row tibble by |
frame.rate |
a numeric value giving the frame rate in frames per second. Only relevant for MicroDVD format.
If |
encoding |
the name of the encoding to be used. Default is " |
The support of WebVTT is basic and experimental.
An object of class subtitles
(see subtitles
).
# read a SubRip file f <- system.file("extdata", "ex_subrip.srt", package = "subtools") f <- system.file("extdata", "ex_webvtt.vtt", package = "subtools") read_subtitles(f)
# read a SubRip file f <- system.file("extdata", "ex_subrip.srt", package = "subtools") f <- system.file("extdata", "ex_webvtt.vtt", package = "subtools") read_subtitles(f)
This function uses mkvextract
to extract subtitles from an MKV file.
You must have mkvmerge
and mkvextract
installed on your computer.
read_subtitles_mkv(file, id = 2, mkvextract.exec = "mkvextract", mkvmerge.exec = "mkvmerge")
read_subtitles_mkv(file, id = 2, mkvextract.exec = "mkvextract", mkvmerge.exec = "mkvmerge")
file |
a character string giving the path to the MKV file. |
id |
An integer giving the ID of the tracks to be extracted.
Can be a vector of length > 1 to extract several tracks.
If |
mkvextract.exec |
a character string giving the path to the |
mkvmerge.exec |
character string giving the path to the |
The function get_mkv_info
is a simple way to identify the ID of subtitles tracks from a MKV file.
Not all the subtitle formats supported by mkvextract
can be read by subtools
.
See read_subtitles
for the list of formats currently supported.
An object of class subtitles
(see subtitles
).
If several tracks are requested (via id
), an object of class multisubtitles
;
i.e. a list of subtitles
objects.
https://mkvtoolnix.download/downloads.html
These functions read one or several subtitles files organized in directories. They are designed to import subtitles of series with multiple episodes.
read_subtitles_season(dir, format = "auto", bind = TRUE, detect.meta = TRUE, quietly = FALSE, ...) read_subtitles_serie(dir, format = "auto", bind = TRUE, detect.meta = TRUE, quietly = FALSE, ...) read_subtitles_multiseries(dir, format = "auto", bind = TRUE, detect.meta = TRUE, quietly = FALSE, ...)
read_subtitles_season(dir, format = "auto", bind = TRUE, detect.meta = TRUE, quietly = FALSE, ...) read_subtitles_serie(dir, format = "auto", bind = TRUE, detect.meta = TRUE, quietly = FALSE, ...) read_subtitles_multiseries(dir, format = "auto", bind = TRUE, detect.meta = TRUE, quietly = FALSE, ...)
dir |
the name of the directory which the subtitles are to be read from (see Details). |
format |
a character string specifying the format of the subtitles
(default is " |
bind |
a logical. If |
detect.meta |
a logical. If |
quietly |
a logical. If |
... |
further arguments to be passed to |
These functions read subtitles files at different levels from a 3-levels directory (see the tree below).
The function read_subtitles_multiseries
reads everything recursively from "Series_Collection".
The function read_subtitles_serie
reads everything recursively from a serie folder (e.g. "Serie_A").
The function read_subtitles_season
reads everything from a season folder (e.g. "Season_1").
To read a specific episode file (e.g. "Episode_1.srt), use read_subtitles
.
Series_Collection |-- Serie_A | |-- Season_1 | | |-- Episode_1.srt |-- Serie_B | |-- Season_1 | | |-- Episode_1.srt | |-- Season_2 | | |-- Episode_1.srt | | |-- Episode_2.srt
If bind
is set on TRUE
a subtitles
object,
otherwise an object of class multisubtitles
;
i.e. a list of subtitles
objects.
subtitles
objectA subtitles
is a special form of tibble
.
subtitles(text, timecode.in, timecode.out, id, metadata = data.frame())
subtitles(text, timecode.in, timecode.out, id, metadata = data.frame())
text |
a character vector of subtitles text content. |
timecode.in |
a character vector giving the time that the subtitles appear on the screen. The format must be "HH:MM:SS.mS". |
timecode.out |
a character vector giving the time that the subtitles disappear. The format must be "HH:MM:SS.mS". |
id |
a vector of IDs for subtitles.
If not provided it is generated automatically from |
metadata |
a one-row dataframe or tibble, or any object that can be coerced
into a one-row tibble by |
a subtitles
object i.e. a tibble
with at least 4 columns containing IDs,
timecodes and text of the subtitles and optionally metadata in extra columns.
This function extends unnest_tokens to subtitles objects. The main difference with the data.frame method is the possibility to perform timecode remapping according to the split of the input column.
## S3 method for class 'subtitles' unnest_tokens(tbl, output, input, token = "words", format = c("text", "man", "latex", "html", "xml"), time.remapping = TRUE, to_lower = TRUE, drop = TRUE, collapse = NULL, ...)
## S3 method for class 'subtitles' unnest_tokens(tbl, output, input, token = "words", format = c("text", "man", "latex", "html", "xml"), time.remapping = TRUE, to_lower = TRUE, drop = TRUE, collapse = NULL, ...)
tbl |
A data frame |
output |
Output column to be created as string or symbol. |
input |
Input column that gets split as string or symbol. The output/input arguments are passed by expression and support quasiquotation; you can unquote strings and symbols. |
token |
Unit for tokenizing, or a custom tokenizing function. Built-in options are "words" (default), "characters", "character_shingles", "ngrams", "skip_ngrams", "sentences", "lines", "paragraphs", "regex", "tweets" (tokenization by word that preserves usernames, hashtags, and URLS ), and "ptb" (Penn Treebank). If a function, should take a character vector and return a list of character vectors of the same length. |
format |
Either "text", "man", "latex", "html", or "xml". If not text, this uses the hunspell tokenizer, and can tokenize only by "word" |
time.remapping |
a logical. If |
to_lower |
Whether to convert tokens to lowercase. If tokens include
URLS (such as with |
drop |
Whether original input column should get dropped. Ignored if the original input and new output column have the same name. |
collapse |
Whether to combine text with newlines first in case tokens (such as sentences or paragraphs) span multiple lines. If NULL, collapses when token method is "ngrams", "skip_ngrams", "sentences", "lines", "paragraphs", or "regex". |
... |
Extra arguments passed on to tokenizers, such
as |
A tibble.
f <- system.file("extdata", "ex_webvtt.vtt", package = "subtools") s <- read_subtitles(f, metadata = data.frame(test = "Test")) require(tidytext) unnest_tokens(s) unnest_tokens(s, Word, Text_content, drop = FALSE) unnest_tokens(s, Word, Text_content, token = "lines")
f <- system.file("extdata", "ex_webvtt.vtt", package = "subtools") s <- read_subtitles(f, metadata = data.frame(test = "Test")) require(tidytext) unnest_tokens(s) unnest_tokens(s, Word, Text_content, drop = FALSE) unnest_tokens(s, Word, Text_content, token = "lines")
This function writes a subtitles
object in a file.
write_subtitles(x, file, format = "srt", encoding = "UTF-8")
write_subtitles(x, file, format = "srt", encoding = "UTF-8")
x |
an object of class |
file |
a character string naming a file for writing. |
format |
a character string giving the file format. Not used (only SubRip format is currently implemented). |
encoding |
the name of the encoding to be used. |