• Python目录操作:
    • 创建目录 :os.mkdir(path)
    • 判断目录存在:os.path.exists(path) 
    • 创建多层目录:os.makedirs(path) 
    • 删除文件:os.remove("文件名")
    • 删除多个文件:
      • 删除多个文件的时候要和glob这个库联合使用
  • '''
    删除合并后的文件
    :param file_path: 
    :return: 
    '''
    def deleteFile(file_path):print("开始删除文件")for infile in glob.glob(os.path.join(file_path, '*.ts')):os.remove(infile)print("文件删除完成")

     

  • Python中glob库的使用:
    • glob库的作用:
      • 匹配特定格式的文件和文件夹。glob库并非是调用shell来实现一个搜索的功能,它是内部调用os.listdir()和fnmatch.fnmatch()来进行实现的。
    • glob库的常用方法:
      • glob(pathname, recursive=False)
        • pathname为需要匹配的字符串。(该参数应尽量加上r前缀,以免发生不必要的错误)
        •  第二个参数代表递归调用,与特殊通配符“**”一同使用,默认为False。
        • 返回值:该通配符可以匹配指定路径里所有文件和目录,包括子目录里的所有文件和目录。
      • iglob(pathname, recursive=False)
        • 返回值: 返回一个迭代器,该迭代器不会同时保存所有匹配到的路径,遍历该迭代器的结果与使用相同参数调用glob()的返回结果一致。
    • glob使用的注意事项:
      • glob默认不匹配以点符号(.)开始的文件,如果有这类文件,则需要做特殊处理。
    • glob的匹配符:
      • glob支持的通配符

将多个ts文件合并成一个mp4文件-编程之家

    • glob的使用例子:
import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']

 

  • FFmpeg的使用
    • FFmpeg的作用:
      • 是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。
      • 它提供了录制、转换以及流化音视频的完整解决方案。
      • 功能:
ffmpeg -y -f concat -safe 0 -i ./vidio/1.txt -c copy ./vidio/测试.mp4
        • 视频截取
ffmpeg -ss 0:0:01 -t 0:20:00 -i 测试.mp4 -vcodec copy -acodec copy ss1.mp4
        • 提取音频和视频:
//分离视频流 
ffmpeg -i 测试.mp4 -vcodec copy -an test1.mp4 
//分离音频流 
ffmpeg -i 测试.mp4 -acodec copy -vn test2.mp4
        • FFmpeg各个参数的使用
主要参数:
-i 设定输入流
-f 设定输出格式
-ss 开始时间
视频参数:
-b 设定视频流量,默认为200Kbit/s
-r 设定帧速率,默认为25
-s 设定画面的宽与高
-aspect 设定画面的比例
-vn 不处理视频
-vcodec 设定视频编解码器,未设定时则使用与输入流相同的编解码器
音频参数:
-ar 设定采样率
-ac 设定声音的Channel数
-acodec 设定声音编解码器,未设定时则使用与输入流相同的编解码器
-an 不处理音频
  • 案例的使用:
    • 在章鱼大数据上下载视频,并且把它进行合并
'''
实现对章鱼大数据网站视频的获取
参数: 网站的m3u8格式
返回: 网站的ts格式文件
'''
import glob
import requests
import osb_url = 'https://jscdn.ipieuvre.com/systemvideo/syx/python/jiapt/0101_h/0101_h.m3u8''''
:param file_url: 
:return: 下载好的ts文件
'''
def dowend_vioid(file_url):file_path = ".\vidio"if not os.path.exists(file_path):os.mkdir(file_path)base_url = '/'.join(b_url.split('/')[0:-1])print(base_url)print("开始写入文件")for u in file_url:url = base_url + '/' + uprint(url)with open(file_path + '/' + '1.txt','a+') as file_txt:file_txt.write('file '+  "F:/Python/Case/大数据实训/章鱼/vidio" + '/' + u )file_txt.write('n')with open(file_path + '/' + u,'wb') as f:f.write(requests.get(url).content)file_txt.close()f.close()mergeFile(file_path,'测试')deleteFile(file_path)print("文件写入完成")'''
对多个ts视频资源进行合并
:param file_path: 
:param outputfile: 
:return: 合并后的mp4视频
'''
def mergeFile(file_path,outputfile):print("开始合并文件")str = r'ffmpeg -y -f concat -safe 0 -i '+ file_path +'\1.txt -c copy '+ file_path +'\'+ outputfile + '.mp4'print(str)os.system(str)print("文件合并完成")'''
删除合并后的文件
:param file_path: 
:return: 
'''
def deleteFile(file_path):print("开始删除文件")for infile in glob.glob(os.path.join(file_path, '*.ts')):os.remove(infile)print("文件删除完成")'''
获取要下载的文件列表
:param url: 
:return: 要获取文件的URL列表
'''
def getAllUrl(url):url_list = []html = requests.get(url)file_url = html.text.split('n')for url in file_url:if len(url) > 0 and url[0] != '#':url_list.append(url)return url_listdef main():file_url = getAllUrl(b_url)dowend_vioid(file_url)if __name__ == '__main__':main()print("程序执行完成")