Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing the demo video #13

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 65 additions & 8 deletions R/demo.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,77 @@
#' @param ... other parameters passed to [av_capture_graphics].
#' @name demo
#' @family av
av_demo <- function(output = "demo.mp4", width = 960, height = 720, framerate = 5, verbose = TRUE, ...){
wonderland <- system.file('samples/Synapsis-Wonderland.mp3', package='av', mustWork = TRUE)
info <- av_video_info(wonderland)
av_demo <- function(output = "demo.mp4", width = 960, height = 720, framerate = 24, verbose = TRUE, ...){

# Attribution:
# Big Horns Intro by Audionautix is licensed under a Creative Commons Attribution license
# (https://creativecommons.org/licenses/by/4.0/)
# Artist: http://audionautix.com/
big_horns_intro <- system.file('samples/Big_Horns_Intro.mp3', package='av', mustWork = TRUE)
info <- av_video_info(big_horns_intro)
len <- framerate * round(info$duration)
res <- round(72 * min(width, height) / 480)

# RColorBrewer::brewer.pal(12,"Set3")
col <- c("#8DD3C7", "#FFFFB3", "#BEBADA", "#FB8072", "#80B1D3", "#FDB462",
"#B3DE69", "#FCCDE5", "#D9D9D9", "#BC80BD", "#CCEBC5", "#FFED6F")
video <- av_capture_graphics(output = output, audio = wonderland, {
# Himmelblau's function: https://en.wikipedia.org/wiki/Himmelblau%27s_function
f <- function(x, y) { (x^2+y-11)^2 + (x+y^2-7)^2 }

x <- seq(-6, 6, length = 100)
y <- x
z <- outer(x, y, f)

# partial derivatives
dx <- function(x,y) {4*x**3-4*x*y-42*x+4*x*y-14}
dy <- function(x,y) {4*y**3+2*x**2-26*y+4*x*y-22}

# gradient descent parameters
num_iter <- len
learning_rate <- 0.0005
x_val <- 6
y_val <- 6

# momentum parameters
beta = 0.95
vdx = 0
vdy = 0

updates_x <- vector("numeric", length = num_iter)
updates_y <- vector("numeric", length = num_iter)
updates_z <- vector("numeric", length = num_iter)

# parameter updates
for (i in 1:num_iter) {

vdx = beta*vdx+dx(x_val,y_val)
vdy = beta*vdy+dy(x_val,y_val)

x_val <- x_val-learning_rate*vdx
y_val <- y_val-learning_rate*vdy
z_val <- f(x_val, y_val)

updates_x[i] <- x_val
updates_y[i] <- y_val
updates_z[i] <- z_val
}

video <- av_capture_graphics(output = output, audio = big_horns_intro, {
for(i in seq_len(len)){
if(as.logical(verbose))
cat(sprintf("\rGenerating plot %d/%d...", i, len), file = stderr())
graphics::hist(stats::rnorm(100), col = col, main = sprintf("frame %d/%d", i, len))


plt <- persp(x, y, z,
theta = -50-i*0.2, phi = 20+log(i),
expand = 0.5,
col = "lightblue", border = 'lightblue',
axes = FALSE, box = FALSE,
ltheta = 60, shade = 0.90
)

points(trans3d(updates_x[1:i], updates_y[1:i], updates_z[1:i], pmat = plt), pch = 16,
cex = c(rep(0.6, i-1), 1.2),
col = c(rep('white', i-1), 'black')
)

}
if(as.logical(verbose))
cat("done!\n", file = stderr())
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ devtools::install_github("thomasp85/gganimate")

## Demo Video

Generate a demo video with some random plots and free [demo music](https://freemusicarchive.org/music/Synapsis/~/Wonderland):
Generate a demo video with some random plots and free [demo music](https://www.youtube.com/watch?v=35sTb_c7fJI):

Music in the demo video:
Big Horns Intro by Audionautix is licensed under a Creative Commons Attribution license (https://creativecommons.org/licenses/by/4.0/)
Artist: http://audionautix.com/

```r
av::av_demo()
```

This demo is totally lame, please open a PR with something better (in base R!).

## Using gganimate

You can use `av_encode_video()` as the renderer in gganimate:
Expand Down
Binary file added inst/samples/Big_Horns_Intro.mp3
Binary file not shown.
Binary file removed inst/samples/Synapsis-Wonderland.mp3
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/testthat/test-video.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ test_that("convert images into video formats", {
})

test_that("audio sampling works", {
wonderland <- system.file('samples/Synapsis-Wonderland.mp3', package='av', mustWork = TRUE)
big_horns_intro <- system.file('samples/Big_Horns_Intro.mp3', package='av', mustWork = TRUE)
for(ext in c("mkv", "mp4", "mov", "flv")){
filename <- paste0("test.", ext)
av::av_encode_video(png_files, filename, framerate = framerate, verbose = FALSE, audio = wonderland)
av::av_encode_video(png_files, filename, framerate = framerate, verbose = FALSE, audio = big_horns_intro)
expect_true(file.exists(filename))
info <- av_video_info(filename)
unlink(filename)
Expand Down