我使用FFMPEG播放视频(因为我需要允许.avi和.mov格式),目前无法找到获取视频持续时间的方法。如果我没有使用FFMPEG,那么使用onMetaData会很简单,但由于FFMPEG需要netStream.play(null);
而不是netStream.play(videoPath);
,因此返回0。
我希望添加的功能,以检查视频的持续时间:
nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
public function onOutputData(e:ProgressEvent):void {
var videoStream:ByteArray = new ByteArray();
nativeProcess.standardOutput.readBytes(videoStream, 0, nativeProcess.standardOutput.bytesAvailable);
netStream.appendBytes(videoStream);
}
答案 0 :(得分:3)
您可以使用ffmpeg
和NativeProcess
var video:File = File.applicationDirectory.resolvePath('video.avi'),
exe:File = File.applicationDirectory.resolvePath('ffmpeg.exe');
if (NativeProcess.isSupported) {
var args:Vector.<String> = new Vector.<String>();
args.push("-i", video.nativePath);
var npsi:NativeProcessStartupInfo = new NativeProcessStartupInfo();
npsi.executable = exe;
npsi.arguments = args;
var process:NativeProcess = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, on_output_data);
process.start(npsi);
function on_output_data(e:ProgressEvent):void {
var s:String = process.standardError.readUTFBytes(process.standardError.bytesAvailable);
var a:Array = s.match(/Duration: (\d{2}:\d{2}:\d{2})/) || [];
if(a.length > 0){
trace('video duration :', a[1]);
// gives for example : video duration : 00:01:45
}
}
} else {
trace('NativeProcess is not supported');
}
你也可以像这样使用ffprobe
:
var exe:File = File.applicationDirectory.resolvePath('ffprobe.exe');
var args:Vector.<String> = new Vector.<String>();
args.push('-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', video.nativePath);
var npsi:NativeProcessStartupInfo = new NativeProcessStartupInfo();
npsi.executable = exe;
npsi.arguments = args;
var process:NativeProcess = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, on_output_data);
process.start(npsi);
function on_output_data(e:ProgressEvent):void
{
trace('video duration :', process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));
// gives for example : video duration : 67.250584
}
希望可以提供帮助。
答案 1 :(得分:1)
编辑(您可以尝试的其他方式..):
当NetStream播放视频时,它首先检查元数据,并且由于FLV元数据包含持续时间条目,您可以尝试读取(在任何实际视频帧数据之前,始终发送元数据以便先附加)...
1) 声明公共var以侦听元数据
public var metaListener : Object;
2) 在设置NetStream时设置Meta侦听器
nc = new NetConnection(); nc.connect(null);
metaListener = new Object();
metaListener = { onMetaData: received_Meta };
ns = new NetStream(nc); ns.client = metaListener;
3) &#34;收到Meta&#34;上面触发的功能&#34; onMetaData&#34;
public function received_Meta ( v_data : Object ) : void
{
trace("video duration : " + v_data.duration );
}
或者自动列出元数据中的每个可用条目
public function received_Meta ( v_data : Object ) : void
{
trace("### Begin Metadata listing : FLV Entries ### " );
for (var entry:* in v_data)
{
var value:Object = v_data[ entry ];
trace(" > " + entry + " : " + value);
}
trace("### End of Metadata listing for this FLV ### " );
}