我使用jcodec从mp4文件中提取帧, 并从图像中创建一个新的视频文件。
但是,我花了很多时间来获取jcodec中的帧总数。 (而且我看过jcodec来源,但我找不到它。)
jcodec是否提供此功能? 或者我应该使用其他库吗?
答案 0 :(得分: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());
答案 1 :(得分: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