1

I am new to Java and I have a project to do, so I have a java file and the user have to choose from a listing of files in a directory. The input from user is saved in a variable (fileName). I want to use that variable in another java file for doing some other work. I searched online but didn't find any solution that works for me. Probably I've done something wrong.

code of the first file:

public class Director {

private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
    is = new FileInputStream(source);
    os = new FileOutputStream(dest);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) > 0) {
        os.write(buffer, 0, length);
    }
} finally {
    is.close();
    os.close();
}
}

public static void main(String[] args) throws IOException {
    
    

    // Creates an array in which we will store the names of files and directories
    String[] pathnames;

    // Creates a new File instance by converting the given pathname string
    // into an abstract pathname
    File f = new File("C:\\Users\\miltos\\Desktop\\polimesa\\available_videos");

    // Populates the array with names of files and directories
    pathnames = f.list();
    System.out.println("Files in the directory:");
    // For each pathname in the pathnames array
    for (String pathname : pathnames) {
        // Print the names of files and directories
        System.out.println(pathname);
    }
    
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter file name");
    String fileName = myObj.nextLine();
    File source = new File("C:\\Users\\miltos\\Desktop\\polimesa\\available_videos\\" + fileName);
    File dest = new File("C:\\Users\\miltos\\Desktop\\polimesa\\raw_videos\\" + fileName);
    copyFileUsingStream(source, dest);


}


}

code of the second file that i want to use the input:

 public class TestFFMpeg {

static Logger log = LogManager.getLogger(TestFFMpeg.class);



public static void main(String[] args) {
    
    
    FFmpeg ffmpeg = null;
    FFprobe ffprobe = null;
    

    
    try {
        log.debug("Initialising FFMpegClient");
        ffmpeg = new FFmpeg("C:\\Users\\miltos\\ffmpeg\\bin\\ffmpeg.exe");
        ffprobe = new FFprobe("C:\\Users\\miltos\\ffmpeg\\bin\\ffprobe.exe");
    } catch (IOException e) {
        e.printStackTrace();
    }

    log.debug("Creating the transcoding");
    FFmpegBuilder builder = new FFmpegBuilder()
            .setInput("C:\\Users\\miltos\\Desktop\\polimesa\\raw_videos\\" + filename) //updated
            .addOutput("C:\\Users\\miltos\\Desktop\\polimesa\\videos\\" + filename) //updated
            .setVideoBitRate(200000) 
            .done();
      log.debug("Creating the executor");
    FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);

    log.debug("Starting the transcoding");
    // Run a one-pass encode
    executor.createJob(builder).run();
    log.debug("Transcoding finished");
}


}
20
  • You can create a constructor in class second , where in you can pass the filename as parameter. Commented Jun 26, 2020 at 14:56
  • I have question to ask , when you have to pass the filename parameter in class one? could you please specify the position? Commented Jun 26, 2020 at 15:01
  • @HarmandeepSinghKalsi I want to pass the filename variable in the input (and output) of the ffmpeg builder. I saw your code. Where do i put the "TestFFMpeg obj = new TestFFMpeg(filename);" ? above the declaration of the class? And when i put the other code you wrote under the declaration of the class, i get errors about the variable filename Commented Jun 26, 2020 at 15:13
  • No,you should write it in class one : TestFFMpeg obj = new TestFFMpeg(filename); because you have the filename in class one right? I think it should be written before copying the files . Before this : copyFileUsingStream(source, dest); I updated the code for the error. Commented Jun 26, 2020 at 15:16
  • Thanks! Now i am getting an error in the second class, where i put the variable filename as input and output (i updated the code on these parts) . The error is:cannot make a static reference to the non-static method Commented Jun 26, 2020 at 15:25

1 Answer 1

1

I created a variable names filename in class second also, which you will pass from the class one , while creating an object of class second like

TestFFMpeg obj = new TestFFMpeg();
obj.methodInSecondClass(filename);

Second Class :

    public class TestFFMpeg {
    
    static Logger log = LogManager.getLogger(TestFFMpeg.class);
    
    public void methodInSecondClass(String filename){

    FFmpeg ffmpeg = null;
        FFprobe ffprobe = null;
        
    
        
        try {
            log.debug("Initialising FFMpegClient");
            ffmpeg = new FFmpeg("C:\\Users\\miltos\\ffmpeg\\bin\\ffmpeg.exe");
            ffprobe = new FFprobe("C:\\Users\\miltos\\ffmpeg\\bin\\ffprobe.exe");
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        log.debug("Creating the transcoding");
        FFmpegBuilder builder = new FFmpegBuilder()
                .setInput("C:\\Users\\miltos\\Desktop\\polimesa\\available_videos\\"+filename) //this is where i want the same variable
                .addOutput("C:\\Users\\miltos\\Desktop\\polimesa\\videos\\"+filename) //this is where i want the same variable
                .setVideoBitRate(200000) 
                .done();
          log.debug("Creating the executor");
        FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
    
        log.debug("Starting the transcoding");
        // Run a one-pass encode
        executor.createJob(builder).run();
        log.debug("Transcoding finished");
    }
    
}
Sign up to request clarification or add additional context in comments.

1 Comment

There is a small update in the code .setInput("C:\\Users\\miltos\\Desktop\\polimesa\\available_videos\\"+filename) .addOutput("C:\\Users\\miltos\\Desktop\\polimesa\\videos\\"+filename) . Please update that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.