Advertisement
Guest User

Apple Segmenter iPhone

a guest
Feb 1st, 2012
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.68 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2009 Chase Douglas
  3.  * Copyright (c) 2012 Antoni Villalonga
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License version 2
  7.  * as published by the Free Software Foundation.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #include "libavformat/avformat.h"
  23.  
  24. static AVStream *add_output_stream(AVFormatContext *output_format_context, AVStream *input_stream) {
  25.     AVCodecContext *input_codec_context;
  26.     AVCodecContext *output_codec_context;
  27.     AVStream *output_stream;
  28.  
  29.     output_stream = av_new_stream(output_format_context, 0);
  30.     if (!output_stream) {
  31.         fprintf(stderr, "Could not allocate stream\n");
  32.         exit(1);
  33.     }
  34.  
  35.     input_codec_context = input_stream->codec;
  36.     output_codec_context = output_stream->codec;
  37.  
  38.     output_codec_context->codec_id = input_codec_context->codec_id;
  39.     output_codec_context->codec_type = input_codec_context->codec_type;
  40.     output_codec_context->codec_tag = input_codec_context->codec_tag;
  41.     output_codec_context->bit_rate = input_codec_context->bit_rate;
  42.     output_codec_context->extradata = input_codec_context->extradata;
  43.     output_codec_context->extradata_size = input_codec_context->extradata_size;
  44.  
  45.     if(av_q2d(input_codec_context->time_base) * input_codec_context->ticks_per_frame > av_q2d(input_stream->time_base) && av_q2d(input_stream->time_base) < 1.0/1000) {
  46.         output_codec_context->time_base = input_codec_context->time_base;
  47.         output_codec_context->time_base.num *= input_codec_context->ticks_per_frame;
  48.     }
  49.     else {
  50.         output_codec_context->time_base = input_stream->time_base;
  51.     }
  52.  
  53.     switch (input_codec_context->codec_type) {
  54.         case CODEC_TYPE_AUDIO:
  55.             output_codec_context->channel_layout = input_codec_context->channel_layout;
  56.             output_codec_context->sample_rate = input_codec_context->sample_rate;
  57.             output_codec_context->channels = input_codec_context->channels;
  58.             output_codec_context->frame_size = input_codec_context->frame_size;
  59.             if ((input_codec_context->block_align == 1 && input_codec_context->codec_id == CODEC_ID_MP3) || input_codec_context->codec_id == CODEC_ID_AC3) {
  60.                 output_codec_context->block_align = 0;
  61.             }
  62.             else {
  63.                 output_codec_context->block_align = input_codec_context->block_align;
  64.             }
  65.             break;
  66.         case CODEC_TYPE_VIDEO:
  67.             output_codec_context->pix_fmt = input_codec_context->pix_fmt;
  68.             output_codec_context->width = input_codec_context->width;
  69.             output_codec_context->height = input_codec_context->height;
  70.             output_codec_context->has_b_frames = input_codec_context->has_b_frames;
  71.  
  72.             if (output_format_context->oformat->flags & AVFMT_GLOBALHEADER) {
  73.                 output_codec_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
  74.             }
  75.             break;
  76.     default:
  77.         break;
  78.     }
  79.  
  80.     return output_stream;
  81. }
  82.  
  83. int main(int argc, char **argv)
  84. {
  85.     const char *input;
  86.     const char *output_prefix;
  87.     double segment_duration;
  88.     char *segment_duration_check;
  89.     double prev_segment_time = 0;
  90.     unsigned int output_index = 1;
  91.     AVInputFormat *ifmt;
  92.     AVOutputFormat *ofmt;
  93.     AVFormatContext *ic = NULL;
  94.     AVFormatContext *oc;
  95.     AVStream *video_st;
  96.     AVStream *audio_st;
  97.     AVCodec *codec;
  98.     char *output_filename;
  99.     char *remove_filename;
  100.     int video_index;
  101.     int audio_index;
  102.     int decode_done;
  103.     int ret;
  104.     int i;
  105.                 int count=1;
  106.  
  107.     if (argc < 4) {
  108.         fprintf(stderr, "Usage: %s <input MPEG-TS file> <segment duration in seconds> <output MPEG-TS file prefix>\n", argv[0]);
  109.         exit(1);
  110.     }
  111.  
  112.     av_register_all();
  113.  
  114.     input = argv[1];
  115.     if (!strcmp(input, "-")) {
  116.         input = "pipe:";
  117.         printf("stdin!\n");
  118.     }
  119.     segment_duration = strtod(argv[2], &segment_duration_check);
  120.     if (segment_duration_check == argv[2] || segment_duration == HUGE_VAL || segment_duration == -HUGE_VAL) {
  121.         fprintf(stderr, "Segment duration time (%s) invalid\n", argv[2]);
  122.         exit(1);
  123.     }
  124.     output_prefix = argv[3];
  125.  
  126.     remove_filename = malloc(sizeof(char) * (strlen(output_prefix) + 15));
  127.     if (!remove_filename) {
  128.         fprintf(stderr, "Could not allocate space for remove filenames\n");
  129.         exit(1);
  130.     }
  131.  
  132.     output_filename = malloc(sizeof(char) * (strlen(output_prefix) + 15));
  133.     if (!output_filename) {
  134.         fprintf(stderr, "Could not allocate space for output filenames\n");
  135.         exit(1);
  136.     }
  137.  
  138.     ifmt = av_find_input_format("mpegts");
  139.     if (!ifmt) {
  140.         fprintf(stderr, "Could not find MPEG-TS demuxer\n");
  141.         exit(1);
  142.     }
  143.  
  144.     ret = av_open_input_file(&ic, input, ifmt, 0, NULL);
  145.     if (ret != 0) {
  146.         fprintf(stderr, "Could not open input file, make sure it is an mpegts file: %d\n", ret);
  147.         exit(1);
  148.     }
  149.  
  150.     if (av_find_stream_info(ic) < 0) {
  151.         fprintf(stderr, "Could not read stream information\n");
  152.         exit(1);
  153.     }
  154.  
  155.     ofmt = av_guess_format("mpegts", NULL, NULL);
  156.     if (!ofmt) {
  157.         fprintf(stderr, "Could not find MPEG-TS muxer\n");
  158.         exit(1);
  159.     }
  160.  
  161.     oc = avformat_alloc_context();
  162.     if (!oc) {
  163.         fprintf(stderr, "Could not allocated output context");
  164.         exit(1);
  165.     }
  166.     oc->oformat = ofmt;
  167.  
  168.     video_index = -1;
  169.     audio_index = -1;
  170.  
  171.     for (i = 0; i < ic->nb_streams && (video_index < 0 || audio_index < 0); i++) {
  172.         switch (ic->streams[i]->codec->codec_type) {
  173.             case CODEC_TYPE_VIDEO:
  174.                 video_index = i;
  175.                 ic->streams[i]->discard = AVDISCARD_NONE;
  176.                 video_st = add_output_stream(oc, ic->streams[i]);
  177.                 break;
  178.             case CODEC_TYPE_AUDIO:
  179.                 audio_index = i;
  180.                 ic->streams[i]->discard = AVDISCARD_NONE;
  181.                 audio_st = add_output_stream(oc, ic->streams[i]);
  182.                 break;
  183.             default:
  184.                 ic->streams[i]->discard = AVDISCARD_ALL;
  185.                 break;
  186.         }
  187.     }
  188.  
  189.     if (av_set_parameters(oc, NULL) < 0) {
  190.         fprintf(stderr, "Invalid output format parameters\n");
  191.         exit(1);
  192.     }
  193.  
  194.     dump_format(oc, 0, output_prefix, 1);
  195.  
  196.     codec = avcodec_find_decoder(video_st->codec->codec_id);
  197.     if (!codec) {
  198.         fprintf(stderr, "Could not find video decoder, key frames will not be honored\n");
  199.     }
  200.  
  201.     if (avcodec_open(video_st->codec, codec) < 0) {
  202.         fprintf(stderr, "Could not open video decoder, key frames will not be honored\n");
  203.     }
  204.  
  205.     snprintf(output_filename, strlen(output_prefix) + 15, "%s%u.ts", output_prefix, output_index++);
  206.     if (url_fopen(&oc->pb, output_filename, URL_WRONLY) < 0) {
  207.         fprintf(stderr, "Could not open '%s'\n", output_filename);
  208.         exit(1);
  209.     }
  210.  
  211.     if (av_write_header(oc)) {
  212.         fprintf(stderr, "Could not write mpegts header to first output file\n");
  213.         exit(1);
  214.     }
  215.  
  216.     do {
  217.         double segment_time;
  218.         AVPacket packet;
  219.  
  220.         decode_done = av_read_frame(ic, &packet);
  221.         if (decode_done < 0) {
  222.             break;
  223.         }
  224.  
  225.         if (av_dup_packet(&packet) < 0) {
  226.             fprintf(stderr, "Could not duplicate packet");
  227.             av_free_packet(&packet);
  228.             break;
  229.         }
  230.  
  231.         if (packet.stream_index == video_index && (packet.flags & PKT_FLAG_KEY)) {
  232.             segment_time = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
  233.                                                 count++;
  234.         }
  235.         else if (video_index < 0) {
  236.             segment_time = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
  237.         }
  238.         else {
  239.             segment_time = prev_segment_time;
  240.         }
  241.  
  242.         if (segment_time - prev_segment_time >= segment_duration) {
  243.                                                 count=1;
  244.             put_flush_packet(oc->pb);
  245.             url_fclose(oc->pb);
  246.  
  247.             snprintf(output_filename, strlen(output_prefix) + 15, "%s%u.ts", output_prefix, output_index++);
  248.             if (url_fopen(&oc->pb, output_filename, URL_WRONLY) < 0) {
  249.                 fprintf(stderr, "Could not open '%s'\n", output_filename);
  250.                 break;
  251.             }
  252.  
  253.             prev_segment_time = segment_time;
  254.         }
  255.  
  256.         ret = av_interleaved_write_frame(oc, &packet);
  257.         if (ret < 0) {
  258.             fprintf(stderr, "Warning: Could not write frame of stream\n");
  259.         }
  260.         else if (ret > 0) {
  261.             fprintf(stderr, "End of stream requested\n");
  262.             av_free_packet(&packet);
  263.             break;
  264.         }
  265.  
  266.         av_free_packet(&packet);
  267.     } while (!decode_done);
  268.  
  269.     av_write_trailer(oc);
  270.  
  271.     avcodec_close(video_st->codec);
  272.  
  273.     for(i = 0; i < oc->nb_streams; i++) {
  274.         av_freep(&oc->streams[i]->codec);
  275.         av_freep(&oc->streams[i]);
  276.     }
  277.  
  278.     url_fclose(oc->pb);
  279.     av_free(oc);
  280.  
  281.                 if ( count==1 ) {
  282.                         remove(output_filename);
  283.                 }
  284.     return 0;
  285. }
  286.  
  287. // vim:sw=4:tw=4:ts=4:ai:expandtab
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement