gpt4 book ai didi

java - 当我尝试删除冗余文件时,即使在关闭所有引用之后,jvm 也会抛出 FileSystemException

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:12 27 4
gpt4 key购买 nike

我正在使用 javacv 创建视频记录文件 (avi)创建后将此文件转换为 mp4 文件 Jaffree 。当我尝试删除现在多余的 avi 文件时,它会抛出 FileSystemException。

我已经释放了所有包含对象的文件,但仍然抛出异常。还尝试使用 apache common io FileUtils.forceDelete(sample.getFile()); 强制删除,但仍然有相同的异常。

<小时/>
public class PacketRecorderTest {

private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd__hhmmSSS");
private static final int RECORD_LENGTH = 10000;
private static final boolean AUDIO_ENABLED = false;
static String inputFile = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov";
static String outputFile = "C:\\Users\\team3\\Desktop\\User\\Vinay\\javacppffmpeg\\frame.avi";

public static void main(String[] args) throws FrameRecorder.Exception, FrameGrabber.Exception, InterruptedException{
packetRecord(inputFile,outputFile);
jafreeToMp4(outputFile, "C:\\Users\\team3\\Desktop\\User\\Vinay\\javacppffmpeg\\frame.mp4");
try {
Files.delete(Paths.get(outputFile));
} catch (IOException e) {
e.printStackTrace();
}
}

public static void packetRecord(String inputFile, String outputFile) throws FrameGrabber.Exception, FrameRecorder.Exception {

FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile);
grabber.start();
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(
outputFile,
1280,
720);
recorder.start(grabber.getFormatContext());
recorder.setFormat("avi");
recorder.setPixelFormat(AV_PIX_FMT_YUV420P);
recorder.setVideoOption("crf", "22");
recorder.setVideoQuality(0);
recorder.setFrameRate(15);
recorder.start();

avcodec.AVPacket packet;
long t1 = System.currentTimeMillis();
while ((packet = grabber.grabPacket()) != null) {
recorder.recordPacket(packet);
if ((System.currentTimeMillis() - t1) > RECORD_LENGTH) {
break;
}
}
grabber.stop();
recorder.stop();
recorder.release();
grabber.release();

}


public static void jafreeToMp4 (String inputFile, String outputFile) {
Path BIN = Paths.get("C:\\ffmpeg\\shared\\bin");
Path VIDEO_MP4 = Paths.get(inputFile);
Path OUTPUT_MP4 = Paths.get(outputFile);

FFmpegResult result = FFmpeg.atPath(BIN)
.addInput(UrlInput.fromPath(VIDEO_MP4))
.addOutput(UrlOutput.toPath(OUTPUT_MP4)
)
.execute();
}

}

<小时/>

java.nio.file.FileSystemException: C:\Users\team3\Desktop\User\Vinay\javacppffmpeg\frame.avi:该进程无法访问该文件,因为该文件正在被另一个进程使用。

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
at java.nio.file.Files.delete(Files.java:1126)
at playground.PacketRecorderTest.main(PacketRecorderTest.java:39)

最佳答案

我无法确定,因为我无法立即访问您正在使用的库。但是,在我看来,您似乎仍然打开了 InputStreamsOutputStreams。我的建议看起来像这样,具体取决于辅助方法的返回类型:

try(InputStream in = UrlInput.fromPath(VIDEO_MP4);
OutputStream out = UrlOutput.toPath(OUTPUT_MP4)) {

FFmpegResult result = FFmpeg.atPath(BIN)
.addInput(in)
.addOutput(out)
.execute();
}

这将在语句完成后关闭资源。

关于java - 当我尝试删除冗余文件时,即使在关闭所有引用之后,jvm 也会抛出 FileSystemException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50217339/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com