Skip to content

Instantly share code, notes, and snippets.

@jgreer
Created January 12, 2011 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgreer/776283 to your computer and use it in GitHub Desktop.
Save jgreer/776283 to your computer and use it in GitHub Desktop.
Segmenter Patch
Index: segmenter.c
===================================================================
--- segmenter.c (revision 50)
+++ segmenter.c (working copy)
@@ -239,7 +239,7 @@
exit(1);
}
- ofmt = guess_format("mpegts", NULL, NULL);
+ ofmt = av_guess_format("mpegts", NULL, NULL);
if (!ofmt) {
fprintf(stderr, "Could not find MPEG-TS muxer\n");
exit(1);
@@ -251,6 +251,9 @@
exit(1);
}
oc->oformat = ofmt;
+
+ // Don't print warnings when PTS and DTS are identical.
+ ic->flags |= AVFMT_FLAG_IGNDTS;
video_index = -1;
audio_index = -1;
@@ -289,6 +292,12 @@
fprintf(stderr, "Could not open video decoder, key frames will not be honored\n");
}
+ if (video_st->codec->ticks_per_frame > 1) {
+ // h264 sets the ticks_per_frame and time_base.den but not time_base.num
+ // since we don't use ticks_per_frame, adjust time_base.num accordingly.
+ video_st->codec->time_base.num *= video_st->codec->ticks_per_frame;
+ }
+
snprintf(output_filename, strlen(output_prefix) + 15, "%s-%u.ts", output_prefix, output_index++);
if (url_fopen(&oc->pb, output_filename, URL_WRONLY) < 0) {
fprintf(stderr, "Could not open '%s'\n", output_filename);
@@ -302,6 +311,10 @@
write_index = !write_index_file(index, tmp_index, segment_duration, output_prefix, http_prefix, first_segment, last_segment, 0, max_tsfiles);
+ // Track initial PTS values so we can subtract them out (removing aduio/video delay, since they seem incorrect).
+ int64_t initial_audio_pts = -1;
+ int64_t initial_video_pts = -1;
+
do {
double segment_time;
AVPacket packet;
@@ -317,14 +330,22 @@
break;
}
- if (packet.stream_index == video_index && (packet.flags & PKT_FLAG_KEY)) {
- segment_time = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
- }
- else if (video_index < 0) {
- segment_time = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
- }
- else {
+ if (packet.stream_index == video_index) {
+ if (initial_video_pts < 0) initial_video_pts = packet.pts;
+ packet.pts -= initial_video_pts;
+ packet.dts = packet.pts;
+ if (packet.flags & AV_PKT_FLAG_KEY) {
+ segment_time = (double)packet.pts * video_st->time_base.num / video_st->time_base.den;
+ } else {
+ segment_time = prev_segment_time;
+ }
+ } else if (packet.stream_index == audio_index) {
+ if (initial_audio_pts < 0) initial_audio_pts = packet.pts;
+ packet.pts -= initial_audio_pts;
+ packet.dts = packet.pts;
segment_time = prev_segment_time;
+ } else {
+ segment_time = prev_segment_time;
}
if (segment_time - prev_segment_time >= segment_duration) {
@@ -357,7 +378,7 @@
prev_segment_time = segment_time;
}
- ret = av_interleaved_write_frame(oc, &packet);
+ ret = av_write_frame(oc, &packet);
if (ret < 0) {
fprintf(stderr, "Warning: Could not write frame of stream\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment