使用Python从.swf中提取视频

nin*_*alf 3 python screen-scraping web-scraping

我编写的代码生成了以下视频的链接.获得后,我尝试以这种方式下载它:

import urllib.request
import os

url = 'http://www.videodetective.net/flash/players/?customerid=300120&playerid=351&publishedid=319113&playlistid=0&videokbrate=750&sub=RTO&pversion=5.2%22%20width=%22670%22%20height=%22360%22'
response = urllib.request.urlopen(url).read()
outpath = os.path.join(os.getcwd(), 'video.mp4')
videofile = open(outpath , 'wb')
videofile.write(response)
videofile.close()   
Run Code Online (Sandbox Code Playgroud)

我得到的只是该目录中一个无法读取的58kB文件.有人能指出我正确的方向吗?

Nik*_* B. 16

使用您的代码,您不会在此处下载编码的视频文件,而是用于播放视频的Flash应用程序(采用CWS格式).它在浏览器中执行并动态加载和播放视频.您需要应用一些逆向工程来确定实际的视频源.以下是我的尝试:

解压缩SWF文件

首先,将您提到的58K文件以名称test.swf(或类似名称)保存在硬盘上.然后,您可以使用小的Perl脚本cws2fws:

perl cws2fws test.swf
Run Code Online (Sandbox Code Playgroud)

这会导致在test.fws.swf同一目录中命名的新文件

在FWS文件中搜索配置URL

我用了一个简单的

strings test.fws.swf | grep http
Run Code Online (Sandbox Code Playgroud)

产量:

...
cookieOhttp://www.videodetective.net/flash/players/flashconfiguration.aspx?customerid=
...
Run Code Online (Sandbox Code Playgroud)

有趣.让我们尝试把我们已知的customerid,playeridpublishedid传递到该网址:

http://www.videodetective.net/flash/players/flashconfiguration.aspx?customerid=300120&playerid=351&publishedid=319113
Run Code Online (Sandbox Code Playgroud)

如果我们在浏览器中打开它,我们可以看到播放器配置XML,它反过来指向我们

http://www.videodetective.net/flash/players/playlist.aspx?videokbrate=450&version=4.6&customerid=300120&fmt=3&publishedid=&sub=
Run Code Online (Sandbox Code Playgroud)

现在,如果我们打开它,我们终于可以看到源URL:

http://cdn.videodetective.net/svideo/mp4/450/6993/293732.mp4?c=300120&r=450&s=293732&d=153&sub=&ref=&fmt=4&e=20111228220329&h=03e5d78201ff0d2f7df9a
Run Code Online (Sandbox Code Playgroud)

现在我们可以下载这个h264视频文件,我们已经完成了.

在Python脚本中自动完成整个过程

这是一项完全不同的任务(留给读者练习).