Play video with sound

Is there a way to incorparate the sound in a movie in one’s sketch

1 Like

please elaborate.

  • Do you mean a simple animation in processing where you play a sound (and produce your own movie from the images)

  • Do you want to load a movie and place a sound in it and then save the movie as a changed file on the hard drive?

1 Like

I want to load a mp4 movie with it’s sound into my sketch. I can then run the sketch and then save it as an mp4 movie. I know how to do the later with screen recording.

If you’re willing to use JavaFX it has a MediaPlayer:

import java.io.File;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

void setup() {
  size(1, 1, FX2D);
  Stage stage = new Stage();
  // **** Replace this with your file path **** //
  String path = "/Users/xxxx/Movies/yyyyy.mp4";
  Media media = new Media(new File(path).toURI().toString());
  MediaPlayer mediaPlayer = new MediaPlayer(media);
  MediaView mediaView = new MediaView(mediaPlayer);
  mediaPlayer.setAutoPlay(true);
  mediaView.setLayoutX(10);
  mediaView.setLayoutY(10);
  Pane pane = new Pane();
  pane.getChildren().add(mediaView);
  Scene scene = new Scene(pane, 500, 500);
  stage.setScene(scene);
  stage.setTitle("Playing video");
  stage.show();
}

Output:

Thanks for the replys. I forgot to mention that my current plans for a solution is to format my sketch so that it had a place to locate my video, and then run the sketch while doing a screen recording.

I later will take the screen recording and the video file and combine the two in my video editor, Premier Elements. I have done trial runs with two mp4 videos and it works fine. I haven’t done it with a video created in Processing yet.

Svan, Where does your code produce an output mp4 file with sound?

Where does your code produce an output mp4 file with sound?
It doesn’t produce anything; it just plays a pre-existing .mp4 video (with sound if it has it). You stated that you already knew how to do a screen recording.

A screen recording willhave poor quality compared to a proper video export.

What hardware acceleration do you have.

There’s no way your CPU can handle playing sound, animating your sketch, and recording the screen simultaneously without really bad performance; that’s not what a CPU is designed to do, that’s why GPUs and dedicated audio cards/chips exist.

Perhaps this may be useful.

Though playing and then re-recording the sound is inefficient and will degrade sound.

Perhaps it makes more sense to simply record your sketch only (no sound), then sperate the audio from the video, and finally combine the sound and sketch animation.

Try these:

ffmpeg -i input-video.mp4 -vn -acodec copy output-audio.aac
ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac output.mp4

You may find this useful: HWAccelIntro – FFmpeg