artclarke / humble-video

Humble Video: Demuxing, Decoding, Filtering, Encoding and Muxing of 100's of video and audio formats and Codecs from the JVM
GNU Affero General Public License v3.0
557 stars 115 forks source link

Question - How do I capture input from a microphone and mix it in with the video? #61

Open gCurtisCT opened 9 years ago

gCurtisCT commented 9 years ago

The included demo is good for video capture, which I have working with the sarxos webcam-capture project, but now I need sound with it.

How do I use the MediaAudio class?

For example, MediaAudo.make() is looking for AudioFormat.Type

https://github.com/artclarke/humble-video/blob/master/humble-video-noarch/src/main/java/io/humble/video/MediaAudio.java#L161

But I can't find "Type" inside of the AudioFormat class at all http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioFormat.html

Might I suggest a RecordAndEncodeAudio demo to compliment the video demo?

jkolobok commented 9 years ago

Use java sound api

gCurtisCT commented 9 years ago

@jkolobok Use the Java sound API instead of the MediaAudio class?

jkolobok commented 9 years ago

Using java sound api you can grab raw audio data from any microphone in your system and then encode it and mix with audio

gCurtisCT commented 9 years ago

@jkolobok I'm attempting to do that, but I'm having trouble making in into an instance of MediaAudio so that I can run Encoder.encode on it. See my original post

gCurtisCT commented 9 years ago

Based on the samples, it seems like the only way to get AudioFormat.Type is to run it through the decoder

https://github.com/artclarke/humble-video/blob/master/humble-video-demos/src/main/java/io/humble/video/demos/DecodeAndPlayAudio.java#L121

Do I have to record my sound, then decode it in order to make it into a MediaAudio format so that I can encode them?

gCurtisCT commented 9 years ago

I figured out that I had to use io.humble.video.AudioFormat.Type, so once I supplied that I was able to make a MediaAudio instance without compilation issues.

Now I have a new problem. When I try to open the Encoder that I use for Audio, my app freezes.

This is how I'm doing it:

Codec acodec = Codec.findEncodingCodec(format.getDefaultAudioCodecId());
Encoder aEncoder = Encoder.make(acodec);
aEncoder.setChannels(1);
aEncoder.setChannelLayout(AudioChannel.Layout.CH_LAYOUT_MONO);
aEncoder.setSampleFormat(fileType); //io.humble.video.AudioFormat.Type.SAMPLE_FMT_S16
aEncoder.setSampleRate((int) aFormat.getSampleRate()); //500
aEncoder.setTimeBase(framerate); //Rational.make(1,24);
aEncoder.setFlag(Encoder.Flag.FLAG_GLOBAL_HEADER,true);
aEncoder.open(null, null);

And it freezes at that last line.

wittenbe commented 9 years ago

For what it's worth, with the same code I'm getting the exception:

  SEVERE: HumbleInvalidArgument::make("Sample rate %ld not supported by encoder.",   (int32_t)sampleRate ): Sample rate 500 not supported by encoder. (Encoder.cpp:151)

It's odd that you seemingly didn't get any log output and/or exception though.

gCurtisCT commented 9 years ago

Oh, thanks. I'm throwing a lot of errors, so maybe I'm just not catching them correctly.

gCurtisCT commented 9 years ago

Now I get this exception, after passing in a valid sample rate

Exception in thread "main" java.lang.IllegalArgumentException: Can only pass complete media to encode

So what is "complete" media?

artclarke commented 9 years ago

Media is complete if it came from a decoder, or it setComplete(...) has been called on it.

On Mon, Mar 9, 2015 at 2:01 PM, gCurtisCT notifications@github.com wrote:

Now I get this exception, after passing in a valid sample rate

Exception in thread "main" java.lang.IllegalArgumentException: Can only pass complete media to encode

So what is "complete" media?

— Reply to this email directly or view it on GitHub https://github.com/artclarke/humble-video/issues/61#issuecomment-77941678 .

gCurtisCT commented 9 years ago

So now I get this error when trying to write my sample to the encoder:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007fffca571889, pid=13732, tid=11904
#
# JRE version: Java(TM) SE Runtime Environment (7.0_45-b18) (build 1.7.0_45-b18)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.45-b08 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [msvcrt.dll+0x1889]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Jim\Desktop\git\java-applet\applet test\test\hs_err_pid13732.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Here's my code: http://pastebin.com/HWbne8mL

Here's the referenced error report file: http://pastebin.com/pYp1H7Mj

gCurtisCT commented 9 years ago

I'll actually make this its own issue^

gCurtisCT commented 9 years ago

@artclarke The issue comes from running setComplete on the sample, and then trying to encode it.

benjdavenport commented 9 years ago

Seeing the same fatal error when attempting to encode audio. Any updates?

KGBaal commented 8 years ago

@gCurtisCT did you get it to work eventually?