---
 libavformat/avformat.h | 30 ++++++++++++++++++++++
 libavformat/mux.c      | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+)

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index f28186f..4449314 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1613,6 +1613,36 @@ void avformat_close_input(AVFormatContext **s);
  * @addtogroup lavf_encoding
  * @{
  */
+
+/**
+ * Open prepare an output AVFormatContext so it can be passed to
+ * avformat_write_header(). Internally avformat_alloc_context() and
+ * avio_open2() will be called.
+ *
+ * @param ps       Pointer to user-supplied AVFormatContext allocated by
+ *                 avformat_alloc_context().
+ *                 May be a pointer to NULL, in which case an AVFormatContext
+ *                 is allocated by this function and written into ps.
+ *                 @note The user-supplied AVFormatContext will be freed
+ *                 on failure. If the supplied context provides the
+ *                 AVIOContext, it will be freed as well.
+ * @param filename Name of the file to create or open.
+ * @param fmt      If non-NULL, this parameter forces a specific input
+ *                 format, otherwise the format is autodetected.
+ * @param options  An AVDictionary filled with AVIOContext and protocol-private
+ *                 options.
+ *                 On return this parameter will be destroyed and replaced
+ *                 with a dict containing options that were not found.
+ *                 May be NULL.
+ *
+ * @return 0 on success, negative AVERROR on failure.
+ *
+ * @see av_opt_find, av_dict_set, avio_open2, avformat_write_header()
+ */
+
+int avformat_open_output(AVFormatContext **ps, const char *filename,
+                         AVOutputFormat *fmt, AVDictionary **options);
+
 /**
  * Allocate the stream private data and write the stream header to
  * an output media file.
diff --git a/libavformat/mux.c b/libavformat/mux.c
index 4067c16..6ce273e 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -235,6 +235,73 @@ fail:
     return ret;
 }
 
+int avformat_open_output(AVFormatContext **ps, const char *filename,
+                         AVOutputFormat *fmt,
+                         AVDictionary **options)
+{
+    AVFormatContext *s = *ps;
+    int ret = 0;
+    AVDictionary *tmp = NULL;
+
+    if (!s && !(s = avformat_alloc_context()))
+        return AVERROR(ENOMEM);
+
+    if (fmt)
+        s->oformat = fmt;
+    else
+        s->oformat = av_guess_format(NULL, filename, NULL);
+
+    if (!s->oformat) {
+        av_log(s, AV_LOG_ERROR,
+               "Unable to find a suitable output format for %s\n",
+               filename);
+        ret = AVERROR(EINVAL);
+        goto fail;
+    }
+
+    if (s->oformat->flags & AVFMT_NEEDNUMBER) {
+        if (!av_filename_number_test(filename)) {
+            ret = AVERROR(EINVAL);
+            goto fail;
+        }
+    }
+
+    av_strlcpy(s->filename, filename, sizeof(s->filename));
+
+    if (options)
+        av_dict_copy(&tmp, *options, 0);
+
+    if ((ret = av_opt_set_dict(s, &tmp)) < 0)
+        goto fail;
+
+    if (!(s->oformat->flags & AVFMT_NOFILE) && !s->pb) {
+        av_dict_set(&tmp, "content_type", s->oformat->mime_type,
+                    AV_DICT_DONT_OVERWRITE);
+
+        if ((ret = avio_open2(&s->pb, filename, AVIO_FLAG_WRITE,
+                              &s->interrupt_callback,
+                              &tmp)) < 0) {
+            goto fail;
+        }
+    }
+
+    if (options) {
+        av_dict_free(options);
+        *options = tmp;
+    }
+
+    *ps = s;
+
+    return 0;
+
+fail:
+    av_dict_free(&tmp);
+    avio_close(s->pb);
+    avformat_free_context(s);
+    *ps = NULL;
+    return ret;
+}
+
 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
 {
     int ret = 0;
-- 
2.1.0

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to