java如何读取mp4内容_如何获取mp4(H.264)的文件信息

1.download DirectShowLib,I use the version DirectShowLibV2-1

its website: http://directshownet.sourceforge.net/

and download page:http://sourceforge.net/projects/directshownet/files/ 2.using the lib in c#

3.code using MediaDet class

reference:

using System.Runtime.InteropServices;

using DirectShowLib;

using DirectShowLib.DES;

code:

public static string GetDuration(string fileName)

{

var mediaDet = (IMediaDet)new MediaDet();

DsError.ThrowExceptionForHR(mediaDet.put_Filename(fileName));

// find the video stream in the file

int index;

var type = Guid.Empty;

for (index = 0; index < 1000 && type != MediaType.Video; index++)

{

mediaDet.put_CurrentStream(index);

mediaDet.get_StreamType(out type);

}

// retrieve some measurements from the video

double frameRate;

mediaDet.get_FrameRate(out frameRate);

var mediaType = new AMMediaType();

mediaDet.get_StreamMediaType(mediaType);

var videoInfo = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));

DsUtils.FreeAMMediaType(mediaType);

var width = videoInfo.BmiHeader.Width;

var height = videoInfo.BmiHeader.Height;

double mediaLength;

mediaDet.get_StreamLength(out mediaLength);

//var frameCount = (int)(frameRate * mediaLength);

//var duration = frameCount / frameRate;

return mediaLength.ToString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java创建FFmpeg进程将文件编码为H.264 / H.265的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class FFmpegEncoder { private static final String FFmpeg_PATH = "path/to/ffmpeg"; // FFmpeg的路径 private static final String INPUT_FILE_PATH = "path/to/input/file"; // 输入文件的路径 private static final String OUTPUT_FILE_PATH = "path/to/output/file"; // 输出文件的路径 private static final String CODEC = "libx264"; // 使用H.264编码器 // private static final String CODEC = "libx265"; // 使用H.265编码器 public static void main(String[] args) { String command = FFmpeg_PATH + " -i " + INPUT_FILE_PATH + " -c:v " + CODEC + " " + OUTPUT_FILE_PATH; Process process = null; try { process = Runtime.getRuntime().exec(command); // 创建FFmpeg进程 InputStream inputStream = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { // 输出FFmpeg进程的输出信息 System.out.println(line); } int exitCode = process.waitFor(); // 等待FFmpeg进程完成 System.out.println("FFmpeg process exited with code " + exitCode); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (process != null) { process.destroy(); // 销毁FFmpeg进程 } } } } ``` 在上面的示例代码中,我们可以指定要使用的编码器,使用`libx264`表示使用H.264编码器,使用`libx265`表示使用H.265编码器。我们可以根据需要进行更改。在执行FFmpeg进程时,我们使用`Runtime.getRuntime().exec()`方法创建进程。然后,我们使用`process.getInputStream()`获取进程的输出流,并使用`BufferedReader`读取输出流中的内容。最后,我们使用`process.waitFor()`等待FFmpeg进程完成,并获取其退出代码。最后,我们使用`process.destroy()`销毁进程以释放资源。 注意:在上面的示例代码中,我们假设您已经正确安装了FFmpeg并将其添加到了系统的环境变量中。如果您的FFmpeg路径与示例代码中的路径不同,请相应地更改路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值