Skip to content

Commit

Permalink
Merge pull request #51 from bcoley/add-audio-url-support
Browse files Browse the repository at this point in the history
Add audio url support
  • Loading branch information
hamoid authored May 21, 2018
2 parents f8cc8c2 + efeaf63 commit 669ddf2
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
59 changes: 59 additions & 0 deletions examples/withAudioUrl/data/withAudioUrl.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import com.hamoid.*;

VideoExport videoExport;

// This sketch creates a movie with audio from a streaming internet
// radio station url. It will run until the 'q' or 'x' keys are pressed.
// To find the url for a given station, look at the .m3u or .pls files
// on the site.

float movieFPS = 30;
boolean copyMode = false;
VideoExport videoExport = null;


void setup() {
background(0);
fill(255);
frameRate(movieFPS);
// video related setup
videoExport = new VideoExport(this);
// video and processing framerates do not have to exactly match,
// since the audio seems to drive the frame creation -
// but looks choppy if they are too different.
videoExport.setFrameRate(movieFPS);
videoExport.setQuality(70, 128);
videoExport.setAudioUrl("http://ice.somafm.com/spacestation");
videoExport.startMovie();
}

void draw() {
// demo drawing
spaceTravel();
// save video frame
videoExport.saveFrame();
}


void keyPressed() {
if (key == 'q' || key == 'x') {
noLoop();
videoExport.endMovie();
exit();
}
}

void spaceTravel() {
stroke(random(127), random(159), random(191), random(127));
strokeWeight(random(2,10));
ellipse(random(width), random(height), random(1,3),random(1,6));
// used to use copy(1, 1, width-2, height-2, 0, 0, width, height);
// processing 3 seemed to introduce artifacts, so this looks better
if (copyMode) {
copy(0, 1, width, height-2, 0, 0, width, height);
}
else {
copy(1, 0, width-2, height, 0, 0, width, height);
}
copyMode = !copyMode;
}
66 changes: 64 additions & 2 deletions src/com/hamoid/VideoExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ public class VideoExport {
public final static String VERSION = "##library.prettyVersion##";
protected final static String SETTINGS_FFMPEG_PATH = "ffmpeg_path";
protected final static String SETTINGS_CMD_ENCODE_VIDEO = "encode_video";
protected final static String SETTINGS_CMD_ENCODE_AUDIO = "encode_audio";
protected final static String SETTINGS_CMD_ENCODE_AUDIO = "encode_audio";
protected final static String SETTINGS_CMD_ENCODE_VIDEO_AUDIO_URL = "encode_video_audio_url";
protected final static String FFMPEG_PATH_UNSET = "ffmpeg_path_unset";
protected static JSONArray CMD_ENCODE_AUDIO_DEFAULT;
protected static JSONArray CMD_ENCODE_VIDEO_DEFAULT;
protected static JSONArray CMD_ENCODE_VIDEO_AUDIO_URL_DEFAULT;
protected final String ffmpegMetadataComment = "Made with " +
"Video Export for Processing - https://git.io/vAXLk";
protected ProcessBuilder processBuilder;
Expand All @@ -71,6 +73,7 @@ public class VideoExport {
protected boolean saveDebugInfo = true;
protected String outputFilePath;
protected String audioFilePath;
protected String audioUrl = null;
protected PImage img;
protected PApplet parent;
protected int ffmpegCrfQuality;
Expand Down Expand Up @@ -158,6 +161,26 @@ public VideoExport(PApplet parent, final String outputFileName,
"[output]" // output file
}));

CMD_ENCODE_VIDEO_AUDIO_URL_DEFAULT = new JSONArray(new StringList(
new String[]{
"[ffmpeg]", // ffmpeg executable
"-y", // overwrite old file
"-f", "rawvideo", // format rgb raw
"-vcodec", "rawvideo", // in codec rgb raw
"-s", "[width]x[height]", // size
"-pix_fmt", "rgb24", // pix format rgb24
"-r", "[fps]", // frame rate
"-i", "-", // pipe input
"-i", "[url]", // to get audio URL
"-acodec", "aac", // audio codec
"-vcodec", "h264", // out codec h264
"-pix_fmt", "yuv420p", // color space yuv420p
"-crf", "[crf]", // quality
"-strict", "experimental", // audio url won't work without this
"-metadata", "comment=[comment]", // comment
"[output]" // output file
}));

CMD_ENCODE_AUDIO_DEFAULT = new JSONArray(new StringList(
new String[]{
"[ffmpeg]", // ffmpeg executable
Expand Down Expand Up @@ -206,12 +229,20 @@ public VideoExport(PApplet parent, final String outputFileName,
settings.setJSONArray(SETTINGS_CMD_ENCODE_AUDIO,
toJSONArray((String) o));
}
// If String, make it JSONArray
o = settings.get(SETTINGS_CMD_ENCODE_VIDEO_AUDIO_URL);
if(o instanceof String) {
settings.setJSONArray(SETTINGS_CMD_ENCODE_VIDEO_AUDIO_URL,
toJSONArray((String) o));
}
} else {
settings = new JSONObject();
settings.setJSONArray(SETTINGS_CMD_ENCODE_VIDEO,
CMD_ENCODE_VIDEO_DEFAULT);
settings.setJSONArray(SETTINGS_CMD_ENCODE_AUDIO,
CMD_ENCODE_AUDIO_DEFAULT);
settings.setJSONArray(SETTINGS_CMD_ENCODE_VIDEO_AUDIO_URL,
CMD_ENCODE_VIDEO_AUDIO_URL_DEFAULT);
}
} catch (URISyntaxException e) {
e.printStackTrace();
Expand Down Expand Up @@ -244,9 +275,33 @@ public void setMovieFileName(final String newMovieFileName) {
outputFilePath = parent.sketchPath(newMovieFileName);
}

/**
* Allow setting a url for audio file name to be used as input.
*
* @param audioFileName
* String file name to be used as audio track for the movie.
*/
public void setAudioFileName(final String audioFileName) {
if (audioUrl != null) {
System.err.println("Can't setAudioFileName() after setAudioUrl()!");
return;
}
audioFilePath = parent.dataPath(audioFileName);
}

/**
* Allow setting a url for audio input.
*
* @param audioUrl
* String with url to be used as audio track for the movie.
*/
public void setAudioUrl(final String audioUrl) {
if (audioFilePath != null) {
System.err.println("Can't setAudioUrl() after setAudioFileName()!");
return;
}
this.audioUrl = audioUrl;
}

/**
* Set the PImage element. Advanced use only. Optional.
Expand Down Expand Up @@ -543,8 +598,14 @@ protected void startFfmpeg(String executable) {

// Get command as JSONArray
JSONArray cmd;
// use video cmd settings based on whether an input audio url is used, or
// if audio we be added possibly from a file in attachSound()
String cmdSelection = SETTINGS_CMD_ENCODE_VIDEO;
if (audioUrl != null) {
cmdSelection = SETTINGS_CMD_ENCODE_VIDEO_AUDIO_URL;
}
try {
cmd = settings.getJSONArray(SETTINGS_CMD_ENCODE_VIDEO);
cmd = settings.getJSONArray(cmdSelection);
} catch (RuntimeException e) {
cmd = CMD_ENCODE_VIDEO_DEFAULT;
}
Expand All @@ -561,6 +622,7 @@ protected void startFfmpeg(String executable) {
.replace("[width]", "" + img.pixelWidth)
.replace("[height]", "" + img.pixelHeight)
.replace("[fps]", "" + ffmpegFrameRate)
.replace("[url]", "" + audioUrl)
.replace("[crf]", "" + ffmpegCrfQuality)
.replace("[comment]", ffmpegMetadataComment)
.replace("[output]", outputFilePath);
Expand Down

0 comments on commit 669ddf2

Please sign in to comment.