2

I'm using MediaRecorder to record video with a MPEG2TS container on a Samsung Galaxy Note 2. It initializes without any errors, and actually writes data to the file (the file grows to several MB). However, the file is not playable in any media player.

Here's my code for initializing the MediaRecorder:

CamcorderProfile profile = null;
if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)){
    profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
}else if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)){
    profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
}else{ profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW); }

myMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
myMediaRecorder.setOutputFormat(8);
myMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
myMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
myMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
myMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);

String f = Environment.getExternalStorageDirectory().toString() + "/video.ts";
myMediaRecorder.setOutputFile(f);
myMediaRecorder.setPreviewDisplay(previewHolder.getSurface());
myMediaRecorder.prepare();
myMediaRecorder.start();

The above code works just fine when I set the output format to MP4 ("2") instead of MPEG2-TS ("8"), but when it's set to 8, it produces an unplayable (but not empty) video!

What could be going on?

Edit: here's a sample video recorded on the device, if anyone's interested.

Dmitry Brant
  • 7,612
  • 2
  • 29
  • 47

2 Answers2

0

"Any media player" is a false claim. I found that VLC was unable to play MPEG2TS streams, but ffplay is able to play them (ffplay video.ts). To debug vlc, you could increase verbosity:

$ vlc -vvv video.ts
...
[0x7ffe34c01728] ts demux debug: eof ?
[0x7ffe34c01728] ts demux warning: lost synchro
[0x7ffe34c01728] ts demux debug: skipping 76 bytes of garbage
[0x7ffe34c01728] ts demux debug: Force Seek Per Percent: Seeking failed at 10%.
[0x7ffe34c01728] ts demux error: libdvbpsi (misc PSI): Bad CRC_32 table 0x0 !!!
[0x7ffe34c01728] ts demux error: libdvbpsi (PAT decoder): invalid section (section_syntax_indicator == 0)
[0x7ffe34c01728] ts demux error: libdvbpsi (PAT decoder): invalid section (section_syntax_indicator == 0)
...

For those who would like to use this format for streaming, be sure to lower the initial probe buffer which is used to detect the file format. 8K works fine for me:

nc -l -p 1337 | ffplay -probesize 8192 -

Or if the stream is not closed properly:

socat TCP-LISTEN:1337,fork,reuseaddr SYSTEM:'killall ffplay; ffplay -probesize 8192 -'
Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
0

The documentation from Android says MPEG_2_TS requires api level 26

https://developer.android.com/reference/android/media/MediaRecorder.OutputFormat.html#MPEG_2_TS

That could be why your video does not play. As at the time of your asking, MPEG_2_TS is not officially supported

DevD'Silva
  • 101
  • 1
  • 1
  • 4
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/17482608) – Chris Travers Sep 29 '17 at 10:56
  • I actually did. The links mentions MPEG_2_TS requires api level 26 which I stated. – DevD'Silva Sep 29 '17 at 11:20