python 简单web音频,使用Selenium Webdriver和Python检测音频

Maybe not easily done, but I want to be able to load a web page in Selenium and test if flash or html5 automatically played audio.

In theory when a user visits my sites they can see the video play but no audio ( correct way)

Sometimes the audio will play automatically with the video, this is what I don't want.

I'm wondering if there is a way to detect if ANY audio is playing via python and firefox? My internal tests will load the page and do quite a few selenium checks to make sure everything is good. What'd I like to do is add a test that also checks with python or selenium if audio is playing and flag it.

Any ideas?

解决方案

I have similar issues. I deal with a lot of audio over the web. I have to automate checks for audio degradation, audio plays, etc. I couldn't really find a library to help me in Groovy or Ruby (I didn't check Python), so I made a call to an installed program (sox) and it would check real time if audio was heard or not.

This was my groovy call/script:

def audioCheck = "sox -t coreaudio Soundflower /Users/me/project/record.wav silence 1 0.1 1% 1 .1 1%".execute()

audioCheck.waitFor()

println "EXIT VALUE FOR SOX IS: ${audioCheck.exitValue()}"

if(audioCheck.exitValue() == 0){

// some stuff would happen now, if exit code is 0

}

About Core Audio and "Soundflower"

The sox call specifies the audio board with the -t coreaudio (On linux you would probably use alsa instead of 'coreaudio' - coreauduio is the OSX audio interface.) The "Soundflower" parameter is the channel I created in Soundflower. This was important to me to record sound in isolation (as I was doing audio quality checks.) For example, if you used the default channel, like alsa default or coreaudio default, it will pick up your mic... so if the guy next to you sneezes... it corrupts the test.

By using a virtual audio channel (like with Soundflower on OSX), you can set all your audio "out" to go out "Soundflower" then have the sox command run on that channel... and you'll only be listening to the system audio coming out the virtual channel.

You can instead use your default channel (but it will end when it hears other noises in the room.)

I ran that code async using groovy tasks... so that code was wrapped in something like:

def listener = task { ... my script above ... }

Since it was listening asynchronously, it wouldn't block the rest of the test.

The Sox command

The actual sox real time command I used, came from this guy's video (check it out as he uses some different parameters):

Extending It

I pushed it a bit further by being able to automate the recording of the audio played through the browser and then use the PESQ algorithm to determine how closely the recorded audio was to the original audio being played. If you're interested in that, feel free to check out my post on it:

Python PCAP Scraping

Maybe unrelated to what you're doing, but I've also played around with using the Pyshark library to rebuild audio from packet captures... it's more complex and perhaps more fragile. But if is of interest, here's my implementation:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值