format/context: implement Context opening as output

This commit is contained in:
lummax 2015-08-23 22:22:27 +02:00
parent 9b612fc90f
commit 471a768b8e
2 changed files with 68 additions and 0 deletions

View File

@ -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 { pub unsafe fn as_ptr(&self) -> *const AVFormatContext {
self.ptr as *const _ self.ptr as *const _
} }

View File

@ -160,6 +160,66 @@ pub fn open_input_as_with<T: AsRef<Path>>(path: &T, format: &Format, options: Di
} }
} }
pub fn open_output<T: AsRef<Path>>(path: &T) -> Result<Context, Error> {
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<T: AsRef<Path>>(path: &T, format: &Format) -> Result<Context, Error> {
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<T: AsRef<Path>>(path: &T, format: &str) -> Result<Context, Error> {
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>) { pub fn dump(ctx: &Context, index: i32, url: Option<&str>) {
let url = url.map(|u| CString::new(u).unwrap()); let url = url.map(|u| CString::new(u).unwrap());