diff --git a/src/format/context.rs b/src/format/context.rs index bf9f66b..9559918 100644 --- a/src/format/context.rs +++ b/src/format/context.rs @@ -22,6 +22,14 @@ impl Context { } } + pub unsafe fn output(ptr: *mut AVFormatContext) -> Self { + Context { + ptr: ptr, + + _input: false, + } + } + pub unsafe fn as_ptr(&self) -> *const AVFormatContext { self.ptr as *const _ } diff --git a/src/format/mod.rs b/src/format/mod.rs index b0f1fd6..a7c0fcb 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -160,6 +160,66 @@ pub fn open_input_as_with>(path: &T, format: &Format, options: Di } } +pub fn open_output>(path: &T) -> Result { + unsafe { + let mut ps = ptr::null_mut(); + let path = from_path(path); + let status = avformat_alloc_output_context2(&mut ps, ptr::null_mut(), ptr::null(), path.as_ptr()); + + match status { + 0 => { + match avio_open(&mut (*ps).pb, path.as_ptr(), AVIO_FLAG_WRITE) { + 0 => Ok(Context::output(ps)), + e => Err(Error::from(e)), + } + }, + e => Err(Error::from(e)) + } + } +} + +pub fn open_output_as>(path: &T, format: &Format) -> Result { + if let &Format::Output(ref format) = format { + unsafe { + let mut ps = ptr::null_mut(); + let path = from_path(path); + let status = avformat_alloc_output_context2(&mut ps, format.as_ptr(), ptr::null(), path.as_ptr()); + + match status { + 0 => { + match avio_open(&mut (*ps).pb, path.as_ptr(), AVIO_FLAG_WRITE) { + 0 => Ok(Context::output(ps)), + e => Err(Error::from(e)), + } + }, + e => Err(Error::from(e)) + } + } + } + else { + Err(Error::Bug) + } +} + +pub fn open_output_as_string>(path: &T, format: &str) -> Result { + unsafe { + let mut ps = ptr::null_mut(); + let path = from_path(path); + let format = CString::new(format).unwrap(); + let status = avformat_alloc_output_context2(&mut ps, ptr::null_mut(), format.as_ptr(), path.as_ptr()); + + match status { + 0 => { + match avio_open(&mut (*ps).pb, path.as_ptr(), AVIO_FLAG_WRITE) { + 0 => Ok(Context::output(ps)), + e => Err(Error::from(e)), + } + }, + e => Err(Error::from(e)) + } + } +} + pub fn dump(ctx: &Context, index: i32, url: Option<&str>) { let url = url.map(|u| CString::new(u).unwrap());