ffmpeg中的探测功能

时间:2014-10-14 14:50:31

标签: c ffmpeg

我正在尝试为STL字幕格式(FFmpeg)编写分路器。我无法理解ffmpeg中如何引用探测函数。 我的探针有以下代码:

#include "avformat.h"
#include "internal.h"
#include "subtitles.h"
#include "libavutil/intreadwrite.h"

typedef struct {
    FFDemuxSubtitlesQueue q;
} STLContext;

static int stl_probe(AVProbeData *p)
{
    char c;
    const unsigned char *ptr = p->buf;
    av_log(0,100,"printing the probe function");
    while(*ptr=='\r' || *ptr=='\n' || *ptr=='$' || (ptr[0]=='/' && ptr[1]=='/'))
        ptr+=ff_subtitles_next_line(ptr);
    if (sscanf(ptr, "%*d:%*d:%*d:%*d , %*d:%*d:%*d:%*d , %c", &c) == 1)
        return AVPROBE_SCORE_MAX;
     return 0;

}
static int64_t get_pts(char **buf, int *duration)
{
        int hh1, mm1, ss1, ms1;
        int hh2, mm2, ss2, ms2;
        int len=0;
        if (sscanf(*buf, "%2d:%2d:%2d:%2d , %2d:%2d:%2d:%2d , %n",
                   &hh1, &mm1, &ss1, &ms1,
                   &hh2, &mm2, &ss2, &ms2, &len) >= 8 && len>0) {
            int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
            int64_t end   = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
            *duration = end - start;
            *buf+=len;
            return start;
         }
        return AV_NOPTS_VALUE;
}

static int stl_read_header(AVFormatContext *s)
{
    STLContext *stl = s->priv_data;
    AVStream *st = avformat_new_stream(s, NULL);

    if (!st)
        return AVERROR(ENOMEM);
    avpriv_set_pts_info(st, 64, 1, 100);
    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
    st->codec->codec_id   = AV_CODEC_ID_TEXT;

    while (!avio_feof(s->pb)) {
        char line[4096];
        char *p = line;
        const int64_t pos = avio_tell(s->pb);
        int len = ff_get_line(s->pb, line, sizeof(line));
        int64_t pts_start;
        int duration;
        if (!len)
            break;

        line[strcspn(line, "\r\n")] = 0;

        pts_start = get_pts(&p , &duration);
        if (pts_start != AV_NOPTS_VALUE) {
            AVPacket *sub;

            sub = ff_subtitles_queue_insert(&stl->q, p, strlen(p), 0);
            if (!sub)
                return AVERROR(ENOMEM);
            sub->pos = pos;
            sub->pts = pts_start;
            sub->duration = duration;
        }
    }

    ff_subtitles_queue_finalize(&stl->q);
    return 0;

}
static int stl_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    STLContext *stl = s->priv_data;
    return ff_subtitles_queue_read_packet(&stl->q, pkt);
}

static int stl_read_seek(AVFormatContext *s, int stream_index,
                             int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
{
    STLContext *stl = s->priv_data;
    return ff_subtitles_queue_seek(&stl->q, s, stream_index,
                                   min_ts, ts, max_ts, flags);
}

static int stl_read_close(AVFormatContext *s)
{
    STLContext *stl = s->priv_data;
    ff_subtitles_queue_clean(&stl->q);
    return 0;
}

AVInputFormat ff_stl_demuxer = {
    .name           = "stl",
    .long_name      = NULL_IF_CONFIG_SMALL("STL subtitles"),
    .priv_data_size = sizeof(STLContext),
    .read_probe     = stl_probe,
    .read_header    = stl_read_header,
    .read_packet    = stl_read_packet,
    .read_seek2     = stl_read_seek,
    .read_close     = stl_read_close,
    .extensions     = "stl",
};

./ffmpeg -f stl -i Test.stl Test.srt

的输出
ffmpeg version N-66838-g9071bab Copyright (c) 2000-2014 the FFmpeg developers
  built on Oct 14 2014 12:28:54 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
  configuration: 
  libavutil      54. 10.100 / 54. 10.100
  libavcodec     56.  4.101 / 56.  4.101
  libavformat    56.  9.100 / 56.  9.100
  libavdevice    56.  1.100 / 56.  1.100
  libavfilter     5.  1.105 /  5.  1.105
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
Input #0, stl, from '/home/eejya/SubtitleTesting/UK_FINAL_240511.stl':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Subtitle: text
Output #0, srt, to '/home/eejya/SubtitleTesting/UkTest.srt':
  Metadata:
    encoder         : Lavf56.9.100
    Stream #0:0: Subtitle: subrip (srt)
    Metadata:
      encoder         : Lavc56.4.101 srt
Stream mapping:
  Stream #0:0 -> #0:0 (text (native) -> subrip (srt))
Press [q] to stop, [?] for help
size=      10kB time=00:27:10.06 bitrate=   0.0kbits/s    
video:0kB audio:0kB subtitle:4kB other streams:0kB global headers:0kB muxing overhead: 121.594238%

当我运行命令

./ffmpeg -i Test.stl Test.srt 

我没有看到av_log打印的任何输出,它表示探测得分非常低,可能导致误检测。这意味着只检查扩展名。因此,我的探测功能未被调用或缓冲区未被读取。 如何检查我的探测功能是否被调用? 低分警告是否意味着该函数被调用?

1 个答案:

答案 0 :(得分:1)

我已根据您所说的更改在本地编译和测试。您的STL分路器已注册:

$ ./ffmpeg -formats
...
 D  stl             STL subtitles
...

正在调用您的探测功能,并且正在使用您的STL分离器。但是,您没有看到打印任何日志条目,因为:

av_log(0, 100, "printing the probe function");
       ^  ^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |  |    |
       |  |    message
       |  |
       |  log level
       |
       context

您的日志级别为100. In FFmpeg, a higher log level means a less severe message, and by default FFmpeg only prints messages of AV_LOG_INFO (32) level or lower。如果您将函数调用更改为:

av_log(NULL, AV_LOG_FATAL, "printing the probe function\n");
// AV_LOG_FATAL is 8

您会看到打印出来的信息。