1

我正在使用 jcodec 从 mp4 文件中提取帧,并从图像中创建一个新的视频文件。

但是,我很难获得 jcodec 中的总帧数。(而且我查看了 jcodec 源,但我找不到它..)

jcodec 提供这个吗?或者我应该使用其他库吗?

4

2 回答 2

1

对我来说,这段代码返回信息:

FileChannelWrapper ch = NIOUtils.readableFileChannel(new File("c:/trans/test.mp4"));
MP4Demuxer demuxer = new MP4Demuxer(ch);
DemuxerTrack video_track = demuxer.getVideoTrack();
System.out.println("video_duration: " + video_track.getMeta().getTotalDuration());
System.out.println("video_frame: " + video_track.getMeta().getTotalFrames());
于 2017-03-28T10:34:50.390 回答
0

也许需要opencv:

import com.googlecode.javacv.cpp.opencv_highgui;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
/**
* sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
* sudo apt-get update
* sudo apt-get install ffmpeg
*There is opencv_ffmpeg_64.dll file in /home/fang/BigDataSoft/opencv-2.4.13/3rdparty/ffmpeg
*/
public class GetFrameFormVideo {
public static void main(String[] args) {
// System.out.println("Welcome to OpenCV " + Core.VERSION);
// System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
// System.out.println("m = " + m.dump());
//Load the local OpenCV library so that you can use it to call the Java API
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
run2();
}
public static void run2() {
//Read video file
VideoCapture cap = new VideoCapture("/home/fang/Downloads/src_11.mp4");
System.out.println(cap.isOpened());
//Determine whether the video is open
if (cap.isOpened()) {

double frameCount = cap.get(opencv_highgui.CV_CAP_PROP_FRAME_COUNT);
System.out.println("Total video frames:"+frameCount);

double fps = cap.get(opencv_highgui.CV_CAP_PROP_FPS);
System.out.println("Video frame rate"+fps);

double len = frameCount / fps;
System.out.println("Total video duration:"+len);
Double d_s = new Double(len);
System.out.println(d_s.intValue());
Mat frame = new Mat();
for (int i = 0; i < d_s.intValue(); i++) {
//Set the position of the video (unit: milliseconds)
cap.set(opencv_highgui.CV_CAP_PROP_POS_MSEC, i * 1000);
//Read the next frame
if (cap.read(frame)) {
System.out.println("Saving");
//Save the picture to the local directory
Highgui.imwrite("/home/fang/images/" + i + ".jpg", frame);
}
}
//Close video file
于 2021-07-08T08:35:49.350 回答