Skip to content

Commit

Permalink
add seamless parm to ov_playlist_to_video v0.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondben committed Nov 29, 2023
1 parent 3d3828b commit 762859c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ovideo
Title: Volleyball Video Utilities
Version: 0.20.0
Version: 0.20.1
Authors@R: c(person(given = "Ben", family = "Raymond", role = c("aut", "cre"), email = "[email protected]"),
person(given = "Adrien", family = "Ickowicz", role = "aut"),
person("openvolley.org", role = "org"))
Expand Down Expand Up @@ -40,4 +40,4 @@ Suggests:
remotes,
testthat
Remotes: openvolley/datavolley, openvolley/ovdata, scienceuntangled/editry
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
15 changes: 1 addition & 14 deletions R/editry.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,7 @@ ov_editry_clips <- function(playlist, title = NULL, title2 = NULL, label_col, pa
if (missing(label_col)) label_col <- NULL
if (!is.null(label_col) && !label_col %in% names(playlist)) label_col <- NULL

if (isTRUE(seamless)) {
## combine adjacent/overlapping clips into one
keep <- rep(FALSE, nrow(playlist)); keep[1] <- TRUE
last <- 1L
for (i in seq_len(nrow(playlist))[-1]) {
if (isTRUE(playlist$video_src[last] == playlist$video_src[i]) && playlist$start_time[last] + playlist$duration[last] >= playlist$start_time[i]) {
playlist$duration[last] <- playlist$start_time[i] + playlist$duration[i] - playlist$start_time[last]
} else {
keep[i] <- TRUE
last <- i
}
}
playlist <- playlist[keep, ]
}
if (isTRUE(seamless)) playlist <- merge_seamless(playlist)

play_clips <- lapply(seq_len(nrow(playlist)), function(i) {
lyrs <- list(editry::er_layer_video(path = playlist$video_src[i], cut_from = playlist$start_time[i], cut_to = playlist$start_time[i] + playlist$duration[i]))
Expand Down
4 changes: 3 additions & 1 deletion R/ffmpeg_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ ov_images_to_video <- function(input_dir, image_file_mask = "image_%06d.jpg", im
#' @param playlist data.frame: a playlist as returned by `ov_video_playlist`. Note that only local video sources are supported
#' @param filename string: file to write to. If not specified (or `NULL`), a file in the temporary directory will be created. If `filename` exists, it will be overwritten. The extension of `filename` will determine the output format
#' @param subtitle_column string: if not `NULL`, a subtitle file will be produced using the contents of this column (in the playlist) as the subtitle for each clip. The subtitle file will have the same name as `filename` but with extension ".srt"
#' @param seamless logical: if `TRUE`, combine overlapping/adjacent clips. Note that if a `subtitle_col` has been specified, the subtitle from the first clip will be used for the whole of the combined clip (so it may no longer make sense, for example if the subtitle is the player name)
#' @param debug logical: if `TRUE`, echo the ffmpeg output to the console
#'
#' @return A list with the filenames of the created video and subtitle files.
Expand Down Expand Up @@ -276,11 +277,12 @@ ov_images_to_video <- function(input_dir, image_file_mask = "image_%06d.jpg", im
#' }
#'
#' @export
ov_playlist_to_video <- function(playlist, filename, subtitle_column = NULL, debug = FALSE) {
ov_playlist_to_video <- function(playlist, filename, subtitle_column = NULL, seamless = FALSE, debug = FALSE) {
ov_ffmpeg_ok(do_error = TRUE)
if (missing(filename) || is.null(filename)) filename <- tempfile(fileext = ".mp4")
execfun <- if (isTRUE(debug)) sys::exec_wait else sys::exec_internal
lapplyfun <- if (!requireNamespace("future.apply", quietly = TRUE) || inherits(future::plan(), "sequential")) lapply else future.apply::future_lapply
if (isTRUE(seamless)) playlist <- merge_seamless(playlist)
tempfiles <- lapplyfun(seq_len(nrow(playlist)), function(ri) {
outfile <- tempfile(fileext = paste0(".", fs::path_ext(playlist$video_src[ri])))
if (file.exists(outfile)) unlink(outfile)
Expand Down
31 changes: 12 additions & 19 deletions R/playlists.R
Original file line number Diff line number Diff line change
Expand Up @@ -554,26 +554,19 @@ render_pl <- function(pl, pl_name, bannerdir, outfile, table_cols, ...) {
}

merge_seamless <- function(playlist) {
## merge seamless transitions into single entry
all_start <- playlist$start_time
all_duration <- playlist$duration
all_end <- all_start + all_duration
current <- 1
to_del <- rep(FALSE, nrow(playlist))
i <- 1
while (i < nrow(playlist)) {
if (all_end[current] >= all_start[i + 1]) {
all_end[current] <- all_end[i + 1]
to_del[i+1] <- TRUE
## combine adjacent/overlapping clips into one
keep <- rep(FALSE, nrow(playlist)); keep[1] <- TRUE
last <- 1L
dur <- playlist$duration
for (i in seq_len(nrow(playlist))[-1]) {
if (isTRUE(playlist$video_src[last] == playlist$video_src[i]) && (playlist$start_time[last] + dur[last] >= playlist$start_time[i])) {
dur[last] <- playlist$start_time[i] + dur[i] - playlist$start_time[last]
} else {
current <- i + 1
keep[i] <- TRUE
last <- i
}
i <- i + 1
}
all_duration <- all_end - all_start
playlist$start_time <- all_start
playlist$seamless_start_time <- all_start
playlist$duration <- all_duration
playlist$seamless_duration <- all_duration
playlist[!to_del, ]
playlist$seamless_duration <- playlist$duration <- dur
playlist$seamless_start_time <- playlist$start_time
playlist[keep, ]
}
10 changes: 9 additions & 1 deletion man/ov_playlist_to_video.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 762859c

Please sign in to comment.