Convert MPEG4 to MPEGTS on Android with FFmpeg -
ok, know little none ffmpeg api when made original post... quite overwhelming when 1 starts learning digital media , conversion details. after reading quite bit more , going through ffmpeg source, able working output mp4 mpegts. concept similar executing:
ffmpeg -i in.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb out.ts but mentioned before, need implement ffmpeg api in c.
so, although able generate playable .ts file, video , audio streams not synced. is, playing them on android tablet plays video while audio playing @ normal speed , (once audio stream ends) video plays @ normal speed end. playing same generated .ts file in vlc produces condensed audio (as though fast-forwarded) , plays video fine.
there still many aspects of media conversion not familiar with. sure of them prevent me successful conversion.
here information (via ffprobe) files: - in.mp4 - file generated via android recording - mpeg4 (h.264 + aac) - ffmpeg.ts - file generated via ffmpeg conversion - mpeg2ts (h.264 + aac) - out.ts - file generated via code - mpegts (h.264 + aac)
in.mp4
filename=in.mp4 nb_streams=2 format_name=mov,mp4,m4a,3gp,3g2,mj2 format_long_name=quicktime/mpeg-4/motion jpeg 2000 format start_time=0:00:00.000000 duration=0:00:09.961383 size=4.730 mibyte bit_rate=3.983 mbit/s tag:major_brand=isom tag:minor_version=0 tag:compatible_brands=isom3gp4 tag:creation_time=2013-05-28 17:06:57 ffmpeg.ts
filename=ffmpeg.ts nb_streams=2 format_name=mpegts format_long_name=mpeg-2 transport stream format start_time=0:00:01.400000 duration=0:00:09.741267 size=5.132 mibyte bit_rate=4.419 mbit/s out.ts
filename=out.ts nb_streams=2 format_name=mpegts format_long_name=mpeg-2 transport stream format start_time=0:00:00.000000 duration=0:00:09.741267 size=5.166 mibyte bit_rate=4.449 mbit/s firstly, unable affect output file's start_time. next, upon examining -show_packets output of probe, saw following:
ffmpeg.ts
[packet] codec_type=video stream_index=0 pts=n/a pts_time=n/a dts=n/a dts_time=n/a duration=0 duration_time=0:00:00.000000 size=20.506 kibyte pos=564 flags=k [/packet] [packet] codec_type=video stream_index=0 pts=n/a pts_time=n/a dts=n/a dts_time=n/a duration=0 duration_time=0:00:00.000000 size=11.727 kibyte pos=22936 flags=_ [/packet] ... [packet] codec_type=audio stream_index=1 pts=126000 pts_time=0:00:01.400000 dts=126000 dts_time=0:00:01.400000 duration=2089 duration_time=0:00:00.023211 size=285.000 byte pos=109416 flags=k [/packet] [packet] codec_type=audio stream_index=1 pts=128089 pts_time=0:00:01.423211 dts=128089 dts_time=0:00:01.423211 duration=2089 duration_time=0:00:00.023211 size=374.000 byte pos=-1 flags=k [/packet] ... [packet] codec_type=video stream_index=0 pts=n/a pts_time=n/a dts=n/a dts_time=n/a duration=0 duration_time=0:00:00.000000 size=20.000 kibyte pos=87232 flags=_ [/packet] [packet] codec_type=video stream_index=0 pts=n/a pts_time=n/a dts=n/a dts_time=n/a duration=0 duration_time=0:00:00.000000 size=16.852 kibyte pos=112800 flags=_ [/packet] out.ts
[packet] codec_type=audio stream_index=1 pts=0 pts_time=0:00:00.000000 dts=0 dts_time=0:00:00.000000 duration=2089 duration_time=0:00:00.023211 size=285.000 byte pos=22936 flags=k [/packet] [packet] codec_type=audio stream_index=1 pts=1024 pts_time=0:00:00.011378 dts=1024 dts_time=0:00:00.011378 duration=2089 duration_time=0:00:00.023211 size=374.000 byte pos=23312 flags=k [/packet] ... [packet] codec_type=video stream_index=0 pts=n/a pts_time=n/a dts=n/a dts_time=n/a duration=0 duration_time=0:00:00.000000 size=11.727 kibyte pos=25004 flags=_ [/packet] [packet] codec_type=audio stream_index=1 pts=7168 pts_time=0:00:00.079644 dts=7168 dts_time=0:00:00.079644 duration=2089 duration_time=0:00:00.023211 size=299.000 byte pos=55460 flags=k [/packet] as can see, ffmpeg.ts starts out video packets not have pts/dts. audio packets follow contain pts/dts. repeats until end. video packets not have pts/dts according ffprobe output.
however, out.ts starts audio packets , alternate video packets. here, video packets not have pts/dts. difference here there 1 video packet between series of audio packets. happened rest of video packets (ffmpeg.ts has ~5 audio followed ~5 video).
obviously, i'm still learning , don't know way yet... jump out obvious problem anyone? appreciate info / suggestions continue grind @ it!!
ok, suspected, obvious not newbie. basically, if not have re-encode packets, streams in original container come own time base per stream. needs converted or "rescaled" new output stream's time base.
packet.pts = av_rescale_q(packet->pts, instream->time_base, outstream->time_base); packet.dts = av_rescale_q(packet->dts, instream->time_base, outstream->time_base); this rescale packets each stream. of course, basic action can taken , additional alternations / rescalings may need performed (is packet doesn't have dts / pts). however, in control of input file should suffice me.
Comments
Post a Comment