[Python] How to execute a python file using SpringBoot

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    this is the imports of my python file called movie.py

    import os
    from moviepy.video.io.VideoFileClip import VideoFileClip
    from moviepy.video.VideoClip import TextClip
    from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
    import subprocess
    import moviepy.config as conf
    from moviepy.audio.io.AudioFileClip import AudioFileClip
    import pyttsx3


    this is located on resources/movie-python

    i tried executing this file on the command line on its directory

    python movie.py


    however executing this on java returns a diffrent result

    public void callPythonFile() {
    try {
    // Directory where the Python script is located
    final File scriptDirectory = new File("src/main/resources/movie-python");


    // Command to run the Python script
    final String command = "python movie.py";

    // Start the process with the script directory as the working directory
    ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
    processBuilder.directory(scriptDirectory);
    Process process = processBuilder.start();


    // Read output from the process
    InputStream inputStream = process.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    while ((line = reader.readLine()) != null) {
    System.out.println(line);
    }

    // Wait for the process to finish
    int exitCode = process.waitFor();
    if (exitCode != 0) {
    throw new RuntimeException("Python script exited with non-zero code: " + exitCode);
    }
    System.out.println("Python script exited with code: " + exitCode);

    } catch (IOException | InterruptedException e) {
    throw new RuntimeException("Error executing Python script: " + e.getMessage(), e);
    }
    }


    this code freezes on the line

    video_with_text.write_videofile(temp_file, codec='libx264', audio_codec='aac')


    why does it has different result on manually executing the file?

    Continue reading...

Compartilhe esta Página