[FFmpeg-devel] [PATCH] ffmpeg.c: copy chapters by default.

Anton Khirnov wyskas
Thu Mar 18 09:40:38 CET 2010


Chapters are copied from the first input file that has them to all
output files.
---
 ffmpeg.c |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index d0af510..f558908 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2134,6 +2134,63 @@ static int av_encode(AVFormatContext **output_files,
                                     in_file->iformat->metadata_conv);
     }
 
+    /* copy chapters from the first input file that has them*/
+    is = NULL;
+    for (i = 0; i < nb_input_files; i++) {
+        if (input_files[i]->nb_chapters) {
+            is = input_files[i];
+            break;
+        }
+    }
+    for (j = 0; j < nb_output_files; j++) {
+        if (!is)
+            break;
+
+        os = output_files[j];
+        for (k = 0; k < is->nb_chapters; k++) {
+            AVChapter *in_ch, *out_ch;
+            AVMetadataTag *t = NULL;
+
+            in_ch  = is->chapters[k];
+            if (av_compare_ts(in_ch->end, in_ch->time_base, start_time - input_files_ts_offset[i],
+                              AV_TIME_BASE_Q) < 0)
+                continue;
+            if (recording_time != INT64_MAX &&
+                av_compare_ts(in_ch->start, in_ch->time_base, recording_time + start_time - input_files_ts_offset[i],
+                              AV_TIME_BASE_Q) > 0)
+                break;
+
+            out_ch = av_mallocz(sizeof(AVChapter));
+            if (!out_ch) {
+                print_error(os->filename, AVERROR(ENOMEM));
+                av_exit(1);
+            }
+
+            out_ch->id        = in_ch->id;
+            out_ch->time_base = in_ch->time_base;
+            if (av_compare_ts(in_ch->start, in_ch->time_base, start_time - input_files_ts_offset[i],
+                              AV_TIME_BASE_Q) < 0)
+                out_ch->start = 0;
+            else
+                out_ch->start = in_ch->start - av_rescale_q(start_time - input_files_ts_offset[i],
+                                                            AV_TIME_BASE_Q, out_ch->time_base);
+            if (recording_time != INT64_MAX &&
+                av_compare_ts(in_ch->end, in_ch->time_base, recording_time + start_time - input_files_ts_offset[i],
+                              AV_TIME_BASE_Q) > 0)
+                out_ch->end   = av_rescale_q(recording_time, AV_TIME_BASE_Q, out_ch->time_base);
+            else
+                out_ch->end   = in_ch->end - av_rescale_q(start_time - input_files_ts_offset[i],
+                                                          AV_TIME_BASE_Q, out_ch->time_base);
+
+            while ((t = av_metadata_get(in_ch->metadata, "", t, AV_METADATA_IGNORE_SUFFIX)))
+                av_metadata_set2(&out_ch->metadata, t->key, t->value, 0);
+
+            os->nb_chapters++;
+            os->chapters = av_realloc(os->chapters, sizeof(AVChapter)*os->nb_chapters);
+            os->chapters[os->nb_chapters - 1] = out_ch;
+        }
+    }
+
     /* open files and write file headers */
     for(i=0;i<nb_output_files;i++) {
         os = output_files[i];
-- 
1.7.0




More information about the ffmpeg-devel mailing list