将RTSP流保存到android中的mp4文件

时间:2015-12-22 12:34:29

标签: android ffmpeg

我正在开发一个项目,我需要从IPCamera中读取输入流。我可以通过rtsp网址获取此内容。

显示IPCamera流。我可以通过使用 -

来做同样的事情
    videoView = (VideoView) this.findViewById(R.id.videoView1);
    MediaController mc = new MediaController(this);
    videoView.setMediaController(mc);
    videoView.setVideoURI(Uri.parse("rtsp://xxxxxxxx/camera1"));
    videoView.requestFocus();

现在我想将此流记录到MP4文件中。同样我使用mediarecorder。我被困住了。

    MediaRecorder mediaRecorder = new MediaRecorder();
    //mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mediaRecorder.setOutputFile("rtsp://xxxxxxxxx/camera1");
    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mediaRecorder.start();

由于

1 个答案:

答案 0 :(得分:0)

使用ffmpeg

  1. 添加ffmpeg依赖项。

    compile 'nl.bravobit:android-ffmpeg:1.1.5'
    
  2. 记录流。

    String RTSP_URL = "rtsp://<your_rtsp_url>";
    
    final File targetFile = new File( getExternalStoragePublicDirectory( Environment.DIRECTORY_MOVIES )  + "/recording1.mp4" );
    
    final FFmpeg ffmpeg = FFmpeg.getInstance(this);
    
    String[] ffmpegCommand = new String[]{  "-i", RTSP_URL, "-acodec", "copy", "-vcodec", "copy", targetFile.toString() };
    
    final FFtask ffTask = ffmpeg.execute( ffmpegCommand, new FFcommandExecuteResponseHandler() {
    
      @Override
      public void onStart() {}
    
      @Override
      public void onProgress(String message) {}
    
      @Override
      public void onFailure(String message) {}
    
      @Override
      public void onSuccess(String message) {}
    
      @Override
      public void onFinish() {}
    
    } );
    
    final Timer timer = new java.util.Timer();
    
    TimerTask timerTask = new TimerTask() { @Override public void run() {
      ffTask.sendQuitSignal();
    } };
    
    timer.schedule( timerTask, 30000 ); // Will stop recording after 30 seconds.