本文介绍了使用Selenium Python的FFMPEG输出总是损坏的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Selenium python运行测试用例,并想在每个测试用例上录制视频,但是当我尝试输出时,输出总是损坏.FFMPEG进程正在运行,在输出行上没有错误出现.我附上我的代码,请任何人帮助我,我需要添加或删除任何内容

i try to running testcase using selenium python and want to record video on every testacases, but when i try the output is always corrupted. FFMPEG process are running, no error appear on the output line. I attach my code, Please anyone help me is there anything i need to add or remove

这是记录器的第一个文件:

here's the first file, for recorder :

import subprocess
from subprocess import Popen
from subprocess import call


class recorderMethod():
    videoRecording = None

    @staticmethod
    def recorder_start(res,name):
        rec_lib          = 'ffmpeg -y -rtbufsize 2000M -f dshow  -i video="screen-capture-recorder" -s '
        resolution       = res
        buffer           = ' -b:v 512k -r 20 -vcodec libx264 '
        filename         = name
        extension        = '.mp4'
        complete_command = rec_lib+resolution+buffer+filename+extension

        recorderMethod.videoRecording = Popen(str(complete_command))

    @staticmethod
    def recorder_stop():
        if recorderMethod.videoRecording.poll() is None:
            call('taskkill /F /T /PID ' + str(recorderMethod.videoRecording.pid))

这是录制视频的主要测试文件

here's the main test file for record the video

import unittest
import recorder_main
from selenium import webdriver
from time import sleep

class recordingTest(unittest.TestCase):
    #init test
    browser         = webdriver.Chrome()
    baseurl         = 'http://www.facebook.com/'
    record          = recorder_main.recorderMethod
    
    #setup
    def setUp(self):
        #declare to use browser
        self.driver = recordingTest.browser
        #make variable for easy access
        driver = self.driver
        #maximize Firefox
        driver.maximize_window()
        #go to maukerja
        driver.get(recordingTest.baseurl)

    #test001
    def test_001_record(self):
        #start recording
        recordingTest.record.recorder_start('1920x1080','Test_Sleep')
        sleep(10)
        #stop_recording
        recordingTest.record.recorder_stop()


    #teardown
    def test_999_ShutDownTest(self):
        self.driver.close()
        
if __name__ == '__main__':
    unittest.main(exit=False)

推荐答案

您需要正常停止FFmpeg:

You need to gracefully stop FFmpeg:

您可以使用以下代码:

import signal

# ...

# https://stackoverflow.com/questions/9722624/how-to-stop-ffmpeg-remotely  
# https://stackoverflow.com/questions/27356837/send-sigint-in-python-to-os-system
def recorder_stop():
    if recorderMethod.videoRecording.poll() is None:
        recorderMethod.videoRecording.send_signal(signal.CTRL_C_EVENT)
        sleep(3)  # To be sure that the process ends  


用于关闭FFmpeg的代码是:


The code you are using for closing FFmpeg is:

call('taskkill /F /T /PID ' + str(recorderMethod.videoRecording.pid))

使用 taskkill 关闭FFmpeg,导致文件损坏.
FFmpeg终止,但未完成将视频流写入输出文件的操作.

Closing FFmpeg using taskkill, results a corrupted file.
FFmpeg is terminated without finish writing the video stream to the output file.

在旧版本的FFmpeg中,按 Q (我不知道年龄多大)来停止录制.
对于新版本,可以通过按 Ctrl-C 停止录制.
假设我们使用的是新版本.

In old versions of FFmpeg recording is stopped by pressing Q (I don't know how old).
For new versions, the recording is stopped by pressing Ctrl-C.
Lets assume we are using a new version.

发送 signal.CTRL_C_EVENT 等同于按Ctrl-C退出.

Sending signal.CTRL_C_EVENT is equivalent to quitting by pressing Ctrl-C.

注意:
如果您希望FFmpeg录制屏幕10秒钟并终止屏幕,则可以添加 -t 10 (输出)参数,而无需执行 recorder_stop().

Note:
In case you want FFmpeg to record the screen for 10 seconds and terminate, you may add -t 10 (output) argument, without executing recorder_stop().

这篇关于使用Selenium Python的FFMPEG输出总是损坏的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:50