public synchronized void createAudioMuxer() throws IllegalStateException{ if (Thread.currentThread().isInterrupted()) { release(); } if ( isExternalStorageWritable()) { File encodedFile = new File(OUTPUT_FILENAME_DIR, "/movies/EncodedAudio.mp4"); if (encodedFile.exists()) { boolean result = encodedFile.delete(); if (!result) throw new IllegalStateException("Unable to delete video file"); } String outputPath = encodedFile.toString(); int format = MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4; try { mAudioMuxer = new MediaMuxer(outputPath, format); } catch (IOException e) { Log.e(TAG, "Audio temp Muxer failed to create!!"); } } }
/******************************************************************************** * Create a MediaMuxer. We can't add the video track and start() the muxer here, * because our MediaFormat doesn't have the Magic Goodies. These can only be * obtained from the encoder after it has started processing data. **********************************************************************************/ @SuppressWarnings("all") private synchronized void createMuxer() { Log.d(TAG, "--->createMuxer()"); if ( isExternalStorageWritable()) { File encodedFile = new File(OUTPUT_FILENAME_DIR, "/movies/EncodedAV" + "-" + mEncodingWidth + "x" + mEncodingHeight + ".mp4"); if (encodedFile.exists()) { encodedFile.delete(); } String outputPath = encodedFile.toString(); int format = MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4; try { mMuxer = new MediaMuxer(outputPath, format); } catch (IOException e) { Log.e(TAG, e.getLocalizedMessage()); } mVideoTrackIndex = -1; mMuxerStarted = false; } }
@Override public void prepare(EncodingConfig config) { super.prepare(config); try { switch (config.getFormat()) { case MPEG4: mMuxer = new MediaMuxer(config.getOutputPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); break; default: throw new IllegalArgumentException("Unrecognized format!"); } } catch (IOException e) { throw new RuntimeException("MediaMuxer creation failed", e); } }
private void prepareEncoder() { mBufferInfo = new MediaCodec.BufferInfo(); MediaFormat format = MediaFormat.createVideoFormat( MIME_TYPE, mSource.getWidth(), mSource.getHeight() ); format.setInteger( MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface ); format.setInteger( MediaFormat.KEY_BIT_RATE, mBitRate ); format.setInteger( MediaFormat.KEY_FRAME_RATE, FRAME_RATE ); format.setInteger( MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL ); mEncoder = MediaCodec.createEncoderByType( MIME_TYPE ); mEncoder.configure( format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE ); mSurface = mEncoder.createInputSurface(); mEncoder.start(); try { mMuxer = new MediaMuxer( mUri.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4 ); } catch ( IOException ioe ) { throw new RuntimeException( "MediaMuxer creation failed", ioe ); } mTrackIndex = -1; mMuxerStarted = false; }
@Override public int addTrack(MediaFormat mediaFormat) { int ret=-1; synchronized (Lock){ if(!muxStarted){ if(audioTrack==-1&&videoTrack==-1){ try { mMuxer=new MediaMuxer(path,MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } catch (IOException e) { e.printStackTrace(); AvLog.e("create MediaMuxer failed:"+e.getMessage()); } } String mime=mediaFormat.getString(MediaFormat.KEY_MIME); if(mime.startsWith("audio")){ audioTrack=mMuxer.addTrack(mediaFormat); ret=audioTrack; }else if(mime.startsWith("video")){ videoTrack=mMuxer.addTrack(mediaFormat); ret=videoTrack; } startMux(); } } return ret; }
@SuppressLint("NewApi") @Override public void run() { try { try { prepareEncoder(); mMuxer = new MediaMuxer(mDstPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } catch (IOException e) { throw new RuntimeException(e); } mVirtualDisplay = mMediaProjection.createVirtualDisplay(TAG + "-display", mWidth, mHeight, mDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, mSurface, null, null); Log.d(TAG, "created virtual display: " + mVirtualDisplay); recordVirtualDisplay(); } finally { release(); } }
/** * Start record a MP4 video. Need be called while stream. * * @param path where file will be saved. * @throws IOException If you init it before start stream. */ public void startRecord(String path) throws IOException { if (streaming) { mediaMuxer = new MediaMuxer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); if (videoFormat != null) { videoTrack = mediaMuxer.addTrack(videoFormat); } if (audioFormat != null) { audioTrack = mediaMuxer.addTrack(audioFormat); } mediaMuxer.start(); recording = true; } else { throw new IOException("Need be called while stream"); } }
/** * Start record a MP4 video. Need be called while stream. * * @param path where file will be saved. * @throws IOException If you init it before start stream. */ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public void startRecord(String path) throws IOException { if (streaming) { mediaMuxer = new MediaMuxer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); if (videoFormat != null) { videoTrack = mediaMuxer.addTrack(videoFormat); } if (audioFormat != null) { audioTrack = mediaMuxer.addTrack(audioFormat); } mediaMuxer.start(); recording = true; } else { throw new IOException("Need be called while stream"); } }
/** * Constructor * * @param ext extension of output file * @throws IOException */ public SohuMediaMuxerManager(String ext) throws IOException { if (TextUtils.isEmpty(ext)) { ext = ".mp4"; } try { // 输出文件路径 mOutputPath = getCaptureFile(ext).toString(); // } catch (final NullPointerException e) { throw new RuntimeException("This app has no permission of writing external storage"); } // 编码器 mMediaMuxer = new MediaMuxer(mOutputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); // mEncoderCount = mStatredCount = 0; // mIsStarted = false; }
/** * Configures encoder and muxer state, and prepares the input Surface. */ public VideoEncoderCore(int width, int height, int bitRate, File outputFile) throws IOException { mBufferInfo = new MediaCodec.BufferInfo(); MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, width, height); // Set some properties. Failing to specify some of these can cause the MediaCodec // configure() call to throw an unhelpful exception. format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate); format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE); format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL); if (VERBOSE) Log.d(TAG, "format: " + format); // Create a MediaCodec encoder, and configure it with our format. Get a Surface // we can use for input and wrap it with a class that handles the EGL work. mEncoder = MediaCodec.createEncoderByType(MIME_TYPE); mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mInputSurface = mEncoder.createInputSurface(); mEncoder.start(); // Create a MediaMuxer. We can't add the video track and start() the muxer here, // because our MediaFormat doesn't have the Magic Goodies. These can only be // obtained from the encoder after it has started processing data. // // We're not actually interested in multiplexing audio. We just want to convert // the raw H.264 elementary stream we get from MediaCodec into a .mp4 file. mMuxer = new MediaMuxer(outputFile.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); mTrackIndex = -1; mMuxerStarted = false; }
public void startRecord() throws IOException { synchronized (REC_LOCK){ isRecordStarted=true; MediaFormat audioFormat=mConfig.getAudioFormat(); mAudioEncoder=MediaCodec.createEncoderByType(audioFormat.getString(MediaFormat.KEY_MIME)); mAudioEncoder.configure(audioFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE); MediaFormat videoFormat=mConfig.getVideoFormat(); mVideoEncoder=MediaCodec.createEncoderByType(videoFormat.getString(MediaFormat.KEY_MIME)); //此处不能用mOutputSurface,会configure失败 mVideoEncoder.configure(videoFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE); mEncodeSurface=mVideoEncoder.createInputSurface(); mAudioEncoder.start(); mVideoEncoder.start(); mMuxer=new MediaMuxer(mOutputPath,MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); mRecordBufferSize = AudioRecord.getMinBufferSize(mRecordSampleRate, mRecordChannelConfig, mRecordAudioFormat)*2; // buffer=new byte[bufferSize]; mAudioRecord=new AudioRecord(MediaRecorder.AudioSource.MIC,mRecordSampleRate,mRecordChannelConfig, mRecordAudioFormat,mRecordBufferSize); mAudioThread=new Thread(new Runnable() { @Override public void run() { mAudioRecord.startRecording(); while (!audioEncodeStep(isTryStopAudio)){}; mAudioRecord.stop(); } }); mAudioThread.start(); isRecordAudioStarted=true; } }
public MediaMuxerWraper(String path, int format) throws IOException { mMuxer=new MediaMuxer(path,format); datas=new LinkedBlockingQueue<>(30); recycler=new Recycler<>(); ThreadFactory factory= Executors.defaultThreadFactory(); mExec=new ThreadPoolExecutor(1,1,1,TimeUnit.MINUTES,new LinkedBlockingQueue<Runnable>(16),factory); }
public MMediaMuxer(File outputFile) { // Create a MediaMuxer. We can't add the video track and start() the muxer here, // because our MediaFormat doesn't have the Magic Goodies. These can only be // obtained from the encoder after it has started processing data. try { mMuxer = new MediaMuxer(outputFile.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); startCount=0; } catch (IOException e) { e.printStackTrace(); } }
/** Constructs a new {@link MpegEncoder} */ private MpegEncoder(@NonNull Builder builder, @NonNull MediaFormat format, @NonNull String path) throws IOException { checkState(); mFrameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE); mEncoder = MediaCodec.createEncoderByType(format.getString(MediaFormat.KEY_MIME)); mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mSurface = mEncoder.createInputSurface(); mInputSurface = InputSurface.create ( mSurface, builder.inputBuffer, builder.width, builder.height ) .autoSwap() .build(); mMuxer = new MediaMuxer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); if (builder.mLocation != null) { mMuxer.setLocation(builder.mLocation.x, builder.mLocation.y); } if (builder.mOrientation != 0) { mMuxer.setOrientationHint(builder.mOrientation); } mOutputBuffers = start(); }
/** * Prepares the video encoder, muxer, and an input surface. */ private void prepareEncoder(File outputFile) throws IOException { mBufferInfo = new MediaCodec.BufferInfo(); MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT); // Set some properties. Failing to specify some of these can cause the MediaCodec // configure() call to throw an unhelpful exception. format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE); format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAMES_PER_SECOND); format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL); if (VERBOSE) Log.d(TAG, "format: " + format); // Create a MediaCodec encoder, and configure it with our format. Get a Surface // we can use for input and wrap it with a class that handles the EGL work. mEncoder = MediaCodec.createEncoderByType(MIME_TYPE); mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mInputSurface = mEncoder.createInputSurface(); mEncoder.start(); // Create a MediaMuxer. We can't add the video track and start() the muxer here, // because our MediaFormat doesn't have the Magic Goodies. These can only be // obtained from the encoder after it has started processing data. // // We're not actually interested in multiplexing audio. We just want to convert // the raw H.264 elementary stream we get from MediaCodec into a .mp4 file. if (VERBOSE) Log.d(TAG, "output will go to " + outputFile); mMuxer = new MediaMuxer(outputFile.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); mTrackIndex = -1; mMuxerStarted = false; }
public SyncMediaMuxer(String output) { try { Log.d(TAG, "SyncMediaMuxer"); mMediaMuxer = new MediaMuxer(output, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } catch (IOException e) { e.printStackTrace(); } isStarted = false; }
/** * Constructor * @param file output file path with file name * @throws IOException */ public MediaMuxerWrapper(String file) throws IOException { try { mOutputPath = file; } catch (final NullPointerException e) { throw new RuntimeException("This app has no permission of writing external storage"); } mMediaMuxer = new MediaMuxer(mOutputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); mEncoderCount = mStatredCount = 0; mIsStarted = false; }
/** * Start record a MP4 video. Need be called while stream. * * @param path where file will be saved. * @throws IOException If you init it before start stream. */ public void startRecord(String path) throws IOException { if (streaming) { mediaMuxer = new MediaMuxer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); videoTrack = mediaMuxer.addTrack(videoFormat); audioTrack = mediaMuxer.addTrack(audioFormat); mediaMuxer.start(); recording = true; } else { throw new IOException("Need be called while stream"); } }
/** * Start record a MP4 video. Need be called while stream. * * @param path where file will be saved. * @throws IOException If you init it before start stream. */ public void startRecord(String path) throws IOException { if (streaming) { mediaMuxer = new MediaMuxer(path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); videoTrack = mediaMuxer.addTrack(videoFormat); mediaMuxer.start(); recording = true; } else { throw new IOException("Need be called while stream"); } }
/** * @param outPath 视频输出路径 * @throws IOException */ public MediaMuxerWrapper(String outPath) throws IOException { mOutputPath = outPath; mMediaMuxer = new MediaMuxer(mOutputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); mEncoderCount = mStartedCount = 0; mIsStarted = false; }
public void prepar(String mOutputPath) { try { mMuxer = new MediaMuxer(mOutputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } catch (Exception e) { throw new RuntimeException(e.toString()); } }
/** * Constructor * @param ext extension of output file * @throws IOException */ public MediaMuxerWrapper(String ext) throws IOException { if (TextUtils.isEmpty(ext)) ext = ".mp4"; try { mOutputPath = getCaptureFile(Environment.DIRECTORY_MOVIES, ext).toString(); } catch (final NullPointerException e) { throw new RuntimeException("This app has no permission of writing external storage"); } mMediaMuxer = new MediaMuxer(mOutputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); mEncoderCount = mStatredCount = 0; mIsStarted = false; }
public MediaMovieRecorder(final String output_path, final boolean audio_recording, final boolean useVideoMuxer) throws IOException { super(output_path); mMuxer = useVideoMuxer ? new VideoMuxer(output_path) // API >= 16 : new MediaMuxerWrapper(output_path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); // API >= 18 new MediaVideoEncoder(this, mMediaCodecCallback); if (audio_recording) { new MediaAudioEncoder(this, mMediaCodecCallback); } hasAudioEncoder = audio_recording; }
public void create(EncoderInfo info, MediaFormat format, File out) { this.info = info; try { encoder = MediaCodec.createEncoderByType(format.getString(MediaFormat.KEY_MIME)); encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); encoder.start(); muxer = new MediaMuxer(out.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Constructor * @param _ext extension of output file * @throws IOException */ public MediaMuxerWrapper(final Context context, final String _ext) throws IOException { String ext = _ext; if (TextUtils.isEmpty(ext)) ext = ".mp4"; try { mOutputPath = FileUtils.getCaptureFile(context, Environment.DIRECTORY_MOVIES, ext, 0).toString(); } catch (final NullPointerException e) { throw new RuntimeException("This app has no permission of writing external storage"); } mMediaMuxer = new MediaMuxer(mOutputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); mEncoderCount = mStatredCount = 0; mIsStarted = false; }
private void setupMuxer() { try { mMuxer = new MediaMuxer( mOutputUri.toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4 ); } catch ( IOException ioe ) { throw new RuntimeException( "MediaMuxer creation failed", ioe ); } }
private AndroidMuxer(String outputFile, Format format){ super(outputFile, format); try { switch(format){ case MPEG4: mMuxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); break; default: throw new IllegalArgumentException("Unrecognized format!"); } } catch (IOException e) { throw new RuntimeException("MediaMuxer creation failed", e); } mStarted = false; }