Changeset 387


Ignore:
Timestamp:
04/01/08 23:04:22 (16 years ago)
Author:
r2d
Message:
  • mpcchap can now dump chapters
Location:
libmpc/trunk/mpcchap
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libmpc/trunk/mpcchap/iniparser.c

    r384 r387  
    517517        /* Empty line */
    518518        sta = LINE_EMPTY ;
    519     } else if (line[0]=='#') {
     519    } else if (line[0]=='#' || line[0]==';') {
    520520        /* Comment line */
    521521        sta = LINE_COMMENT ;
  • libmpc/trunk/mpcchap/mpcchap.c

    r384 r387  
    3838#include "iniparser.h"
    3939
    40 // #include <sys/types.h>
    4140#include <sys/stat.h>
    42 // #include <unistd.h>
    4341
    4442// tags.c
     
    5755static void usage(const char *exename)
    5856{
    59         printf("Usage: %s <infile.mpc> <chapterfile.ini>\n", exename);
    60 }
    61 
    62 int main(int argc, char **argv)
    63 {
    64         mpc_reader reader;
    65         mpc_demux* demux;
    66         mpc_streaminfo si;
    67         char * mpc_file, * chap_file;
     57        printf(
     58                "Usage: %s <infile.mpc> <chapterfile.ini>\n"
     59                "   if chapterfile.ini exists, chapter tags in infile.mpc will be\n"
     60                "   replaced by those from chapterfile.ini, else chapters will be\n"
     61                "   dumped to chapterfile.ini\n"
     62                "   chapterfile.ini is something like :\n"
     63                "       [chapter_start_sample]\n"
     64                "       SomeKey=Some Value\n"
     65                "       SomeOtherKey=Some Other Value\n"
     66                "       [other_chapter_start]\n"
     67                "       YouKnowWhatKey=I think you start to understand ...\n"
     68                , exename);
     69}
     70
     71mpc_status add_chaps(char * mpc_file, char * chap_file, mpc_demux * demux, mpc_streaminfo * si)
     72{
     73        struct stat stbuf;
    6874        FILE * in_file;
    69         mpc_status err;
    70         int i;
    71 
    72         if (argc != 3)
    73                 usage(argv[0]);
    74        
    75         mpc_file = argv[1];
    76         chap_file = argv[2];
    77 
    78         err = mpc_reader_init_stdio(&reader, argv[1]);
    79         if(err < 0) return !MPC_STATUS_OK;
    80 
    81         demux = mpc_demux_init(&reader);
    82         if(!demux) return !MPC_STATUS_OK;
    83         mpc_demux_get_info(demux,  &si);
    84 
    85         if (si.stream_version < 8) {
    86                 fprintf(stderr, "this file cannot be edited, please convert it first to sv8 using mpc2sv8\n");
    87                 exit(!MPC_STATUS_OK);
    88         }
    89 
    90         int chap_nb = mpc_demux_chap_nb(demux);
    91         for (i = 0; i < chap_nb; i++) {
    92                 printf("chap %i : %i\n", i+1, mpc_demux_chap(demux, i, 0, 0));
    93         }
    94         int chap_pos = (demux->chap_pos >> 3) + si.header_position;
    95         int end_pos = mpc_demux_pos(demux) >> 3;
    96         int chap_size = end_pos - chap_pos;
    97         printf("chap-size : %i, end_pos : %x\n", chap_size, end_pos);
    98         struct stat stbuf;
    99         stat(argv[1], &stbuf);
     75        int chap_pos, end_pos, chap_size, i;
     76
     77        chap_pos = (demux->chap_pos >> 3) + si->header_position;
     78        end_pos = mpc_demux_pos(demux) >> 3;
     79        chap_size = end_pos - chap_pos;
     80
     81        stat(mpc_file, &stbuf);
    10082        char * tmp_buff = malloc(stbuf.st_size - chap_pos - chap_size);
    10183        in_file = fopen( mpc_file, "r+b" );
     
    10385        fread(tmp_buff, 1, stbuf.st_size - chap_pos - chap_size, in_file);
    10486        fseek(in_file, chap_pos, SEEK_SET);
    105        
     87
    10688        dictionary * dict = iniparser_load(chap_file);
    10789
     
    11294                char * chap_sec = iniparser_getsecname(dict, i);
    11395                mpc_int64_t chap_pos = atoll(chap_sec);
     96                if (chap_pos > si->samples - si->beg_silence)
     97                        fprintf(stderr, "warning : chapter %i starts @ %lli after the end of the stream (%lli)\n", i + 1, chap_pos, si->samples - si->beg_silence);
    11498                nitem = iniparser_getnkey(dict, i);
    11599                for (j = 0; j < nitem; j++) {
     
    131115                FinalizeTags(in_file, TAG_VERSION, TAG_NO_FOOTER | TAG_NO_PREAMBLE);
    132116        }
    133        
     117
    134118        fwrite(tmp_buff, 1, stbuf.st_size - chap_pos - chap_size, in_file);
    135119        ftruncate(fileno(in_file), ftell(in_file));
     120
    136121        fclose(in_file);
    137122        free(tmp_buff);
    138123        iniparser_freedict(dict);
    139         return 0;
    140 }
     124
     125        return MPC_STATUS_OK;
     126}
     127
     128mpc_status dump_chaps(mpc_demux * demux, char * chap_file, int chap_nb)
     129{
     130        int i;
     131        unsigned int tag_size;
     132        FILE * out_file;
     133        char * tag;
     134
     135        if (chap_nb <= 0)
     136                return MPC_STATUS_OK;
     137
     138        out_file = fopen(chap_file, "wb");
     139        if (out_file == 0)
     140                return !MPC_STATUS_OK;
     141
     142        for (i = 0; i < chap_nb; i++) {
     143                int item_count, j;
     144                fprintf(out_file, "[%lli]\n", mpc_demux_chap(demux, i, &tag, &tag_size));
     145                item_count = tag[8] | (tag[9] << 8) | (tag[10] << 16) | (tag[11] << 24);
     146                tag += 24;
     147                for( j = 0; j < item_count; j++){
     148                        int key_len = strlen(tag + 8);
     149                        int value_len = tag[0] | (tag[1] << 8) | (tag[2] << 16) | (tag[3] << 24);
     150                        fprintf(out_file, "%s=%.*s\n", tag + 8, value_len, tag + 9 + key_len);
     151                        tag += 9 + key_len + value_len;
     152                }
     153                fprintf(out_file, "\n");
     154        }
     155
     156        fclose(out_file);
     157
     158        return MPC_STATUS_OK;
     159}
     160
     161int main(int argc, char **argv)
     162{
     163        mpc_reader reader;
     164        mpc_demux* demux;
     165        mpc_streaminfo si;
     166        char * mpc_file, * chap_file;
     167        mpc_status err;
     168        FILE * test_file;
     169
     170        if (argc != 3)
     171                usage(argv[0]);
     172
     173        mpc_file = argv[1];
     174        chap_file = argv[2];
     175
     176        err = mpc_reader_init_stdio(&reader, mpc_file);
     177        if(err < 0) return !MPC_STATUS_OK;
     178
     179        demux = mpc_demux_init(&reader);
     180        if(!demux) return !MPC_STATUS_OK;
     181        mpc_demux_get_info(demux,  &si);
     182
     183        if (si.stream_version < 8) {
     184                fprintf(stderr, "this file cannot be edited, please convert it first to sv8 using mpc2sv8\n");
     185                exit(!MPC_STATUS_OK);
     186        }
     187
     188        int chap_nb = mpc_demux_chap_nb(demux);
     189
     190        test_file = fopen(chap_file, "rb" );
     191        if (test_file == 0) {
     192                err = dump_chaps(demux, chap_file, chap_nb);
     193        } else {
     194                fclose(test_file);
     195                err = add_chaps(mpc_file, chap_file, demux, &si);
     196        }
     197
     198        mpc_demux_exit(demux);
     199        mpc_reader_exit_stdio(&reader);
     200        return err;
     201}
Note: See TracChangeset for help on using the changeset viewer.