介绍
相信储存视频是大家一直纠结的问题,去哪里存, 哪里速度快,安全性怎么样,稳定性又怎么样
现在他来了,对视频切片储存到阿里云图床的方法来了!
那么今天就给大家带来由两位大佬所写的上传脚本
请大家善待此接口
准备
大家可以使用任意系统,确保系统上有安装PHP或是python3以方便运行脚本!
演示
由于操作过于简单,所以并不提供图片教程!
第一步安装php或者python
//安装php //Ubuntu 或 Debian sudo apt install php -y //Centos sudo yum install php -y //安装python //Ubuntu 或 Debian sudo apt install python3 -y //Centos sudo yum install python3 -y
有的时候可能系统会自带python可以输入
python
进行测试PHP脚本创建与使用
我们现创建文件对代码进行储存
//如果没有vim请先安装vim vi darkperson.php 然后把以下内容复制进文件
<?php $v_path = $argv[1]; //切片路径 $v_name = $argv[2]; //带切片的视频路径名称 $s = 5; //切片秒 ts 切片必须小于 5MB if (empty($v_path) || empty($v_name)) { echo "请填写完整参数"; exit; } if ($v_path == '/' || $v_path == '\') { $v_path = ''; } else { mkFolder($v_path); $v_path = $v_path . "/"; } //这是 FFmpeg 处理命令大家自行更改 exec("ffmpeg -i $v_name -c copy -map 0 -f segment -segment_list " . $v_path . "playlist.m3u8 -segment_time $s " . $v_path . "player%03d.ts"); $m = file_get_contents('./' . $v_path . 'playlist.m3u8'); preg_match_all('/player(.*?)\.ts/', $m, $arr); foreach ($arr[1] as $key => $value) { echo "处理第" . $value . '个切片' . "\n"; $ali = upload('./' . $v_path . 'player' . $value . '.ts'); $m = str_replace('player' . $value . '.ts', $ali, $m); file_put_contents('./' . $v_path . 'play.m3u8', $m); } echo "处理完毕" . "\n"; echo "播放链接为:/" . $v_path . 'play.m3u8'; function upload($file) { $post['file'] = file_path($file); $post['scene'] = 'aeMessageCenterV2ImageRule'; $post['name'] = 'player.jpg'; $rel = get_curl('https://kfupload.alibaba.com/mupload', $post, 'iAliexpress/6.22.1 (iPhone; iOS 12.1.2; Scale/2.00)'); $rel = json_decode($rel, true); return $rel['url']; } function get_curl($url, $post = 0, $ua = 0) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // 不验证证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 最大执行时间 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); curl_setopt($ch, CURLOPT_TIMEOUT, 120); $httpheader[] = "Accept:application/json"; $httpheader[] = "Accept-Encoding:gzip,deflate,sdch"; $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8"; $httpheader[] = "Connection:close"; $ip = mt_rand(48, 140) . "." . mt_rand(10, 240) . "." . mt_rand(10, 240) . "." . mt_rand(10, 240); //随机 ip $httpheader[] = 'CLIENT-IP:' . $ip; $httpheader[] = 'X-FORWARDED-FOR:' . $ip; curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } if ($ua) { curl_setopt($ch, CURLOPT_USERAGENT, $ua); } else { curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0"); } curl_setopt($ch, CURLOPT_ENCODING, "gzip"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $ret = curl_exec($ch); curl_close($ch); return $ret; } function mkFolder($path) { if (!is_readable($path)) { is_file($path) or mkdir($path, 0700); } } function file_path($file) { if (class_exists('CURLFile')) { return $post['file'] = new \CURLFile(realpath($file)); } else { return $post['file'] = '@' . realpath($file); } }
代码内有中文注释,大家可以根据自己的情况来设置!
完成后,按下shift+:输入wq对文件进行保存
不要忘记对文件进行权限设置,为了方便,可以设置为777
chmod 777 darkperson.php
大家可以把视频与脚本放置于同一目录,也可以储存于不同目录
然后运行一下命令
php darkperson.php out 01.mp4
out是指输出ts文件存放的位置文件夹,后面就是要切片视频文件所处位置
等待脚本运行完毕,就可在目录找到输出文件目录及名称
处理完毕 播放链接为:/out/play.m3u8
Python脚本创建与使用
首先对脚本进行创建
//如果没有vim请先安装vim vi darkperson.php 然后把以下内容复制进文件
#!/usr/bin/python3 # -*- coding: UTF-8 -*- import requests, os from urllib3 import encode_multipart_formdata from concurrent.futures import ThreadPoolExecutor, as_completed def m_upload(filename): fakename = os.path.splitext(filename)[0] + '.jpg' payload = {'scene':'aeMessageCenterV2ImageRule', 'name':fakename, 'file': (fakename,open(filename,'rb').read())} encode_data = encode_multipart_formdata(payload) data = encode_data[0] headers['Content-Type'] = encode_data[1] for _ in range(3): try: r = requests.post(url, headers=headers, data=data, timeout = 20) except: print('Failed to upload ' + filename) continue if r and 'url' in r.text: print(filename + " upload") return r.json()['url'] else: print('Failed to upload ' + filename) return filename + ' ERROR' if __name__ == '__main__': for file in os.listdir(): if '.m3u8' in file: m3u8 = open(file) break new_m3u8 = open('output.m3u8', 'w') headers = {'user-agent':'iAliexpress/6.22.1 (iPhone; iOS 12.1.2; Scale/2.00)', 'Accept':'application/json', 'Accept-Encoding':'gzip,deflate,sdch', 'Connection':'close'} url = 'https://kfupload.alibaba.com/mupload' file_upload = {t.strip():'' for t in m3u8.readlines() if t[0]!='#'} m3u8.seek(0) executor = ThreadPoolExecutor(max_workers=8) futures = {executor.submit(m_upload, filename):filename for filename in file_upload.keys()} for future in as_completed(futures): file_upload[futures[future]] = future.result() for line in m3u8: if line[0] != '#': new_m3u8.write(file_upload[line.strip()] + '\n') else: new_m3u8.write(line) print("Complete")
由于这个脚本并没有提供自动切片,所以需要大家自行添加或是手动切片!
在视频切片完后 运行脚本,等待完成即可
python3 darkperson.py
输出文件名为:output.m3u8
大家也可以自定参数为
timeout//参数越大运行速度越快 ThreadPoolExecutor//数值越大运行线程越大,越小越稳定!
注意:这个接口只能上次小于 5MB 以下的图片 所以切片完文件大小必须小于 5MB