the ffmpeg recording does not stop after the test case is successfully executed,

I am new to selenium, I want to add screen recording in my test cases, and I am using ffmpeg. The issue i am facing is the recording successfully starts, but it does not stop after the test case is successfully executed.

Below is how my code is strcutured:

package utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ScreenRecording {

    private Process ffmpeg;
    private String outputFilePath;
    private boolean isRecording;

    public ScreenRecording(String outputFilePath) {
        this.isRecording = false;
        this.outputFilePath = outputFilePath;
    }

    public void startRecording() throws IOException {
        String command = String.format(
                "ffmpeg -y -f gdigrab -framerate 30 -probesize 100M -analyzeduration 10M -i desktop -vf "crop=1920:1080:0:0" -c:v libx264 -preset ultrafast -qp 0 -pix_fmt yuv420p %s",
                outputFilePath
        );
        System.out.println("Executing command: " + command); // Print command for debugging
        try {
            ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command);
            builder.redirectErrorStream(true); // Combine error and output streams
            ffmpeg = builder.start();
            isRecording = true;

            // Log output and errors
            new Thread(() -> {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(ffmpeg.getInputStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        } catch (IOException e) {
            e.printStackTrace(); // Print exception for debugging
            throw e; // Rethrow exception to propagate it to the caller
        }
    }

    public void stopRecording() {
        if (ffmpeg != null) {
            ffmpeg.destroy();
            System.out.println("Recording stopped.");
            isRecording = false;
        }
    }

    public boolean isRecording() {
        return isRecording;
    }
}

public class AccountFeatures extends BaseTest {

    @Test(testName = "Register User")
    public void testRegisterUserScenario() throws InterruptedException {
        HomePage homePage = new HomePage(driver);
        LoginPage loginPage = new LoginPage(driver);
        SignUpPage signUpPage = new SignUpPage(driver);
        homePage.clickOnLogin();
        Assert.assertEquals(loginPage.getSingupText(), "New User Signup!");
        loginPage.enterUserName(randomName);
        loginPage.enterUserEmail(randomEmail);
        loginPage.clickOnSignUpButton();
        signUpPage.clickOnTitle();
        signUpPage.enterPassword(randomPassword);
        signUpPage.selectDate(birthDay);
        signUpPage.selectMonth("December");
        signUpPage.selectYear();
        signUpPage.selectFromCountryDropDown("Canada");
    }
}
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import utilities.ScreenRecording;
import webdrivers.DriverManager;

import java.io.IOException;

public class BaseTest {

    protected WebDriver driver;
    private ScreenRecording recorder;

    @BeforeMethod
    public void setUp() throws IOException {
        // Initialize WebDriver (you can choose Chrome, Firefox, etc.)
        String outputFilePath = "D:\Projects\screen_recordings\screen_recording.mp4";
        recorder = new ScreenRecording(outputFilePath);
        recorder.startRecording();
        driver = DriverManager.getDriver();
        driver.get(DataConfig.baseUrl);
        driver.manage().window().maximize();
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
        if (recorder != null && recorder.isRecording()) {
            recorder.stopRecording();
        }
    }
}

I tried different solutions but it does not work at all. Before that i was using @AfterTest annotations but it was giving stack overflow exception.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật