LCOV - code coverage report
Current view: top level - libavformat - webvttenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 38 86.8 %
Date: 2017-12-14 01:15:32 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Matthew Heaney
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : /**
      22             :  * @file
      23             :  * WebVTT subtitle muxer
      24             :  * @see http://dev.w3.org/html5/webvtt/
      25             :  */
      26             : 
      27             : #include "avformat.h"
      28             : #include "internal.h"
      29             : 
      30          74 : static void webvtt_write_time(AVIOContext *pb, int64_t millisec)
      31             : {
      32             :     int64_t sec, min, hour;
      33          74 :     sec = millisec / 1000;
      34          74 :     millisec -= 1000 * sec;
      35          74 :     min = sec / 60;
      36          74 :     sec -= 60 * min;
      37          74 :     hour = min / 60;
      38          74 :     min -= 60 * hour;
      39             : 
      40          74 :     if (hour > 0)
      41           0 :         avio_printf(pb, "%"PRId64":", hour);
      42             : 
      43          74 :     avio_printf(pb, "%02"PRId64":%02"PRId64".%03"PRId64"", min, sec, millisec);
      44          74 : }
      45             : 
      46           1 : static int webvtt_write_header(AVFormatContext *ctx)
      47             : {
      48           1 :     AVStream     *s = ctx->streams[0];
      49           1 :     AVCodecParameters *par = ctx->streams[0]->codecpar;
      50           1 :     AVIOContext *pb = ctx->pb;
      51             : 
      52           1 :     if (ctx->nb_streams != 1 || par->codec_id != AV_CODEC_ID_WEBVTT) {
      53           0 :         av_log(ctx, AV_LOG_ERROR, "Exactly one WebVTT stream is needed.\n");
      54           0 :         return AVERROR(EINVAL);
      55             :     }
      56             : 
      57           1 :     avpriv_set_pts_info(s, 64, 1, 1000);
      58             : 
      59           1 :     avio_printf(pb, "WEBVTT\n");
      60           1 :     avio_flush(pb);
      61             : 
      62           1 :     return 0;
      63             : }
      64             : 
      65          37 : static int webvtt_write_packet(AVFormatContext *ctx, AVPacket *pkt)
      66             : {
      67          37 :     AVIOContext  *pb = ctx->pb;
      68             :     int id_size, settings_size;
      69             :     uint8_t *id, *settings;
      70             : 
      71          37 :     avio_printf(pb, "\n");
      72             : 
      73          37 :     id = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER,
      74             :                                  &id_size);
      75             : 
      76          37 :     if (id && id_size > 0)
      77           0 :         avio_printf(pb, "%.*s\n", id_size, id);
      78             : 
      79          37 :     webvtt_write_time(pb, pkt->pts);
      80          37 :     avio_printf(pb, " --> ");
      81          37 :     webvtt_write_time(pb, pkt->pts + pkt->duration);
      82             : 
      83          37 :     settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS,
      84             :                                        &settings_size);
      85             : 
      86          37 :     if (settings && settings_size > 0)
      87           0 :         avio_printf(pb, " %.*s", settings_size, settings);
      88             : 
      89          37 :     avio_printf(pb, "\n");
      90             : 
      91          37 :     avio_write(pb, pkt->data, pkt->size);
      92          37 :     avio_printf(pb, "\n");
      93             : 
      94          37 :     return 0;
      95             : }
      96             : 
      97             : AVOutputFormat ff_webvtt_muxer = {
      98             :     .name              = "webvtt",
      99             :     .long_name         = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
     100             :     .extensions        = "vtt",
     101             :     .mime_type         = "text/vtt",
     102             :     .flags             = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
     103             :     .subtitle_codec    = AV_CODEC_ID_WEBVTT,
     104             :     .write_header      = webvtt_write_header,
     105             :     .write_packet      = webvtt_write_packet,
     106             : };

Generated by: LCOV version 1.13