Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

ffmpeg - Save a RTSP stream to mp4 file in android

I am working on a project where I need to read input stream from an IPCamera. I am able to fetch this in through an rtsp url.

Display the IPCamera stream. I am able to do same by using -

    videoView = (VideoView) this.findViewById(R.id.videoView1);
    MediaController mc = new MediaController(this);
    videoView.setMediaController(mc);
    videoView.setVideoURI(Uri.parse("rtsp://xxxxxxxx/camera1"));
    videoView.requestFocus();

Now I want to record this stream to an MP4 file. For same I am using mediarecorder.Here I am stuck.

    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();

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use ffmpeg.

  1. Add ffmpeg dependency.

    compile 'nl.bravobit:android-ffmpeg:1.1.5'
    
  2. Record Stream.

    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.
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...