Skip to content

Commit

Permalink
Add trim parameter to av_video_images()
Browse files Browse the repository at this point in the history
Fixes #59
  • Loading branch information
jeroen committed Jan 29, 2025
1 parent b4fd8ea commit fa4f5e4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ jobs:
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
args: 'c("--no-manual", "--as-cran", "--run-dontrun")'
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ URL: https://ropensci.r-universe.dev/av,
https://docs.ropensci.org/av/
BugReports: https://github.com/ropensci/av/issues
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
SystemRequirements: FFmpeg (>= 3.2); with at least libx264 and lame (mp3) drivers.
MacOS Homebrew: ffmpeg. Debian/Ubuntu: libavfilter-dev. Fedora/RHEL: either
Expand All @@ -24,6 +24,7 @@ Depends: R (>= 3.5)
Imports: graphics
Config/pkgdown: seewave, ggplot2, phonTools, signal, tuneR
Suggests:
curl,
testthat,
ps,
ggplot2,
Expand Down
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
0.9.4
- Windows: use ffmpeg from Rtools when available
- Relax unit tests for libavfilter-free-devel on RedHat/Fedora
- av_video_images() gains a trim parameter
- Some internal fixes to support trim filters

0.9.3
- Fixes for ffmpeg 7.1
Expand Down
14 changes: 12 additions & 2 deletions R/images.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@
#' @param video an input video
#' @param destdir directory where to save the png files
#' @param fps sample rate of images. Use `NULL` to get all images.
#' @param trim string value for [ffmpeg trim filter](https://ffmpeg.org/ffmpeg-filters.html#trim)
#' for example `"10:15"` for seconds or `"start_frame=100:end_frame=110"` for frames.
#' @param format image format such as `png` or `jpeg`, must be available from `av_encoders()`
av_video_images <- function(video, destdir = tempfile(), format = 'jpg', fps = NULL){
#' @examples \dontrun{
#' curl::curl_download('https://jeroen.github.io/images/blackbear.mp4', 'blackbear.mp4')
#' av_video_images('blackbear.mp4', fps = 1, trim = "10:20")
#' }
av_video_images <- function(video, destdir = tempfile(), format = 'jpg', fps = NULL, trim = NULL){
stopifnot(length(video) == 1)
vfilter <- ifelse(length(fps), paste0('fps=fps=', fps), 'null')
filter_fps <- if(length(fps)) paste0('fps=fps=', fps)
filter_trim <- if(length(trim)) paste0('trim=', trim)
vfilter <- paste(c(filter_trim, filter_fps), collapse = ', ')
if(vfilter == "")
vfilter <- 'null'
framerate <- av_media_info(video)$video$framerate
dir.create(destdir)
codec <- switch(format, jpeg = 'mjpeg', jpg = 'mjpeg', format)
Expand Down
1 change: 1 addition & 0 deletions av.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 653ad251-3091-4d6a-9c2a-9ef1fbb435bf

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
17 changes: 16 additions & 1 deletion man/av_video_images.Rd

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

2 changes: 1 addition & 1 deletion src/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,9 @@ static int encode_output_frames(output_container *output){
if(ret == AVERROR(EAGAIN))
return 0;
if(ret == AVERROR_EOF){
output->early_end = 1; //trim filter can EOF before input is fully drained
bail_if_null(output, "filter did not return any frames");
bail_if(avcodec_send_frame(output->video_encoder, NULL), "avcodec_send_frame (flush video)");
output->early_end = 1; //trim filter can EOF before input is fully drained
} else {
bail_if(ret, "av_buffersink_get_frame");
if(output->muxer == NULL)
Expand Down

0 comments on commit fa4f5e4

Please sign in to comment.