Class: Chapter

Inherits:
Object
  • Object
show all
Defined in:
ext/chapter/chapter.c

Class Method Summary collapse

Class Method Details

.chapters(path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'ext/chapter/chapter.c', line 6

static VALUE
rb_get_chapters(VALUE self, VALUE path) {
    char* fileName = StringValuePtr(path);
    MP4FileHandle file = MP4Read(fileName, 0);
    if(file == MP4_INVALID_FILE_HANDLE )
    {
       rb_raise(rb_eException, "could not load file");
       return Qnil;
    }

    MP4Chapter_t * chapters = 0;
    uint32_t chapterCount = 0;

    // get the list of chapters
    MP4ChapterType chtp = MP4GetChapters(file, &chapters, &chapterCount, MP4ChapterTypeAny);

    uint32_t i;
    uint32_t pos = 0;
    VALUE result = rb_ary_new();
    for (i = 0; i < chapterCount; ++i)
    {
      VALUE chapter = rb_hash_new();
      // print the infos
      rb_hash_aset(chapter, rb_str_new2("title"), rb_str_new2(chapters[i].title));
      rb_hash_aset(chapter, rb_str_new2("duration"), INT2NUM(chapters[i].duration));
      rb_hash_aset(chapter, rb_str_new2("position"), INT2NUM(pos));
      rb_ary_push(result, chapter);
      pos = pos + chapters[i].duration;
    }
    // free up the memory
    MP4Free(chapters);
    MP4Close(file);
    return result;
}

.set_chapters(path, chapters) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/chapter/chapter.c', line 41

static VALUE
rb_set_chapters(VALUE self, VALUE path, VALUE chapters) {
    char* fileName = StringValuePtr(path);
    MP4FileHandle file = MP4Modify(fileName, 0, 0);
    if(file == MP4_INVALID_FILE_HANDLE )
    {
       rb_raise(rb_eException, "could not load file");
       return Qnil;
    }
    uint32_t chapter_len = RARRAY_LEN(chapters);
    uint32_t i;
    MP4Chapter_t newChapters[chapter_len]; 
    MP4Chapter_t *chapter = malloc(sizeof(MP4Chapter_t));
    for (i = 0; i < chapter_len; i++) {

        VALUE duration = rb_hash_aref(rb_ary_entry(chapters, i), rb_str_new2("duration"));
        VALUE title = rb_hash_aref(rb_ary_entry(chapters, i), rb_str_new2("title"));

        chapter->duration = NUM2UINT(duration);
        strcpy(chapter->title, StringValuePtr(title));
        newChapters[i] = *chapter;
    }
    free(chapter);

    MP4SetChapters(file, &newChapters[0], chapter_len, MP4ChapterTypeQt);
    MP4Close(file);
    return Qnil;
}