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

时间:2018-05-07 15:09:38

标签: java filesystems javacv

在创建将此文件转换为javacv的mp4文件后,我创建了一个Jaffree的视频记录文件(avi)。当我尝试删除现在冗余的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)

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

两次调用start()方法recorder.start(grabber.getFormatContext());start()。因此,第一次调用可能会使引用未被跟踪。