×

MPV播放器系列(一)-剪辑在线视频

hqy hqy 发表于2024-10-08 15:06:48 浏览23 评论0

抢沙发发表评论

一. 软件介绍:

MPV 是一款免费,开源,跨平台的多媒体播放软件,它功能丰富,界面小巧简洁无广告,整体认知度等同于Windows上的PotPlayer. 相对于PotPlayer它最大的特点是跨平台,支持动态lua脚本解析.可高度定制化.市面上不少流行的播放器使用的内核其实也都是MPV,所以不用置疑啦.

MPV 通过配置文件的方式提供了丰富的配置参数,相当方便批量配置,同时本身提供了事件处理接口,因此我们可以自定义lua事件函数,并为函数绑定快捷键,来实现在观看视频时方便的按下键盘上几个字符直接完成一系列的处理. 官网链接如下:

mpv.iompv.io/

本文中,我们通过示例来讲解如何使用mpv播放器实现边看视频边剪辑视频的效果.有的同学可能会说剪辑软件不都有这些基础功能吗? 没错,但你要知道同样的工作,MPV直接也能做,还不用专业的剪辑软件那么复杂,而且这只是我们学习MPV的第一课,其它更强大的功能我会在后续的文中更多的向大家介绍,如果你感兴趣,请先点赞同或关注一下我啦... 哈哈<(*).(*)>

在本文中,我们实现对在线的视频免下载,直接进行剪辑处理,常见的如爱优腾/B站/YouTuBe,及任何其它能拿到视频播放地址的视频,具体怎么去拿地址很简单的,不用去写代码的...详见后文.本地磁盘的视频更不用提,绝对可以的! 当然这里会依赖部分其它软件的功能,但都很简单,问题不大.

二. 示例操作步骤:

本文演示环境为: (MacOS)

1.下载mpv及基本配置

## 1.安装 home brew################################################ 方案一: 终端运行以下脚本安装brew,如果你已安装可忽略$ /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"## 方案二: https://blog.csdn.net/Mr_Yang__/article/details/81131203################################################ 2.修改brew的源为国内的源$ cd "$(brew --repo)"$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"$ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git$ brew update############################################### 通过brew安装mpv$ brew install mpv ## 查看安装的版本信息$ mpv -version mpv 0.33.1 Copyright 2000-2020 # mpv 的默认配置文件$ ls -a /usr/local/Cellar/mpv/0.33.1/share/doc/mpv/. input.conf mpv.conf restore-old-bindings.conf.. mplayer-input.conf mpv.html## 创建目录放置自己的配置(优先级比系统默认配置高)$ mkdir -p ~/.config/mpv/scripts## 拷贝一份系统配置至自己的配置目录$ cp -r -/usr/local/Cellar/mpv/0.33.1/share/doc/mpv/* ~/.config/mpv/################### 拷贝## 说明:## 所有的*.lua脚本放在scripts目录中,mpv打开时会自动加载并解析## 在~/.config/mpv/input.conf中添加快捷键信息## 在~/.config/mpv/mpv.conf中配置mpv播放器信息## 进入自己的配置目录,修改配置文件$ cd ~/.config/mpv/#################

2.配置视频剪辑功能

下载mpv-video-cutter 项目,解压并拷贝内容至~/.config/mpv/scripts,下载github地址链接为:

## 安装ffmpeg$ brew install ffmpeg## 下载并解压git clone https://github.com/rushmj/mpv-video-cutter.git## 拷贝解压内容至如下目录~/.config/mpv/scripts

其它更详细的说明,请参考如下的作者链接:

拷贝后结果为如下框选,总共就3个文件

3.实现在线截取支持

原作者的脚本在使用mpv播放本地视频进行操作剪辑时完全正常,作者也只测试了本地,我自己测试了对线上播放视频的剪辑功能,添加了注释以及为了满足部分功能,作了些兼容修改.所以如果你要实现线上视频剪辑的话请用我的脚本覆盖一下你从GitHub 上下载的文件,如果你只是本地视频剪辑使用,可以忽略这一步! 拷贝操作时只要用如下c_concat.sh文件覆盖同名就行:

#### 文件名称:c_concat.sh 拷贝如下:####cp c_concat.sh ~/.config/mpv/scripts #### 下面为sh文件内容: 经测试:支持优酷/B站/爱奇艺/其它站点*m3u8源#output a run.sh that real cut and concat the file#use ffmpeg,thanks for its developerif [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]then echo "please input paramenter:\$1[time_pairs_txt],\$2[src_media_file],\$3[out_save_path]" exit 2fitime_pairs_txt=$1 # txt file that contain time suits for trim## 格式: local file: ~/super_star.mp4 | web file: http[s]://cdn.ali.upcs.com/hud/zzz_yyyy_mm.mp4?a=b&c=222 | m388 ts: http[s]://cdn.ali.upcs.com/hud/zzz_yyyy/mm/m3u8?a=b&c=222 input=$2 # 待处理视频完整路径 #input=${input//' '/'\ '}input=`printf %q "$input"`output='date "+%Y_%m_%d_%H_%M_%S"' # 剪辑后输出文件名前缀dir_name=.cut_video # 用于临时使用的目录dir_path=$3 # 剪辑后输出文件保存目录sh_dir=$4 # 脚本工作路径IFS=$'/' f=($2) # 对待处理视频的完整路径进行/分割file_name=${f[${#f[*]}-1]} # /后分割得到文件名称## add by likey## 参考: https://blog.csdn.net/weixin_30363981/article/details/97631778http_schemal="http:" https_schemal="https:"echo "----------begin:----------"echo "input name:$input"echo "file name:$file_name"## 如果请求web网页资源,并为http或https协议# if [[ $input == *$http_schemal* || $input == *$https_schemal* ]]if [[ $input =~ "$http_schemal" || $input =~ "$https_schemal" ]]then echo "request web resource!" ## 添加referer referer_str=$'' if [[ $input =~ "iqiyi.com" ]] then # referer_str="-headers Referer:https://www.iqiyi.com/" echo ">>>暂未对爱奇艺设置referer!" elif [[ $input =~ "bilivideo.com" ]] then referer_str="-headers Referer:https://www.bilibili.com/" echo ">>>已经添加到 B 站:referer!" else # referer_str='-headers Referer:https://www.xxxx.com/' echo ">>>input输入流的站点未识别!" fi echo $referer_str ## 丢弃包括?后的内容 file_name=${file_name%%\?*} ## \r\n分割headers中多个参数,不然有警告:No trailing CRLF found in HTTP header. Adding it. st_headers=$referer_str ## 要用正则换成每次对应请求的域名,这里暂时直接写死了,因为除了B站,其它站点似乎不受影响 st_user_agent=$'-user_agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.76 " ' # st_user_agent_m=$' -user_agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 " 'else echo "request local resource!" st_headers=$'' st_user_agent=$'' st_user_agent_m=$''fi## 如文件名不含.号,则不是本地文件和远程文件直链,而是网页ts视频流if [[ $file_name != *$'.'* ]]then echo "hint: maybe online stream, please use you-get tool to get a direct remote media file [mp4|flv|...] ......." ## 设置返回视频格式为mp4及指定一个最终的合并文件名,更改后缀名为mp4不然m3u8下载的多个ts流无法合并转换成功 file_format=$"mp4" file_name=$"m3u8_out_compact.mp4"## 如果请求文件是.m3u8后缀,更改后缀名为mp4不然可能无法转换成功elif [[ $file_name == *$'.m3u8'* || $file_name == *$'.m4s'* || $file_name == *$'.ts'* ]]then file_format=$"mp4" file_name="${file_name}.mp4"else ## 带文件后缀(非.mu38流)的真实视频地址 echo "direct file name: $file_name"fi#file_name=${file_name//' '/'\ '}IFS=$'.' t=($file_name)file_format=${t[${#t[*]}-1]}echo "file name:$file_name, file format:$file_format"# cat $time_pairs_txt | while read line# do# IFS=',' seg=($line)# left=${seg[0]}# right=${seg[1]}# echo "left:$left,right:$right"# doneecho "dir_path for save_file:$dir_path"## add by likey 设置视频统一的输出路径,不然默认会放置在本地文件同目录下,(注意:Web网页请求时地址要处理,不然报错,因为传入的目录是一长串网址)dir_path=$'/Users/Likey/mpv-www-gen'eval cd "$sh_dir"echo "sh_dir for script:`pwd` "echo "cd \"$dir_path\"" > run.shecho "mkdir $dir_name" >> run.shIFS=$'\n' a=(`eval cat $time_pairs_txt`)num=${#a[@]}for i in ${!a[@]};do IFS=',' seg=(${a[i]}) left=${seg[0]} right=${seg[1]} duration=$(echo "scale=2;$right-$left" |bc) # echo "[$i]left:$left,right:$right" #gen a new run.sh # if [ $i = 0 ] # then # echo "echo \"file 'clip$i.mkv'\" >>$dir_name/concat.txt" >run.sh # fi echo "ffmpeg $st_headers $st_user_agent -ss $left -i $input -t $duration $dir_name/clip$i.$file_format" >>run.sh #echo "ffmpeg -y -accurate_seek -ss $left -t $duration -i $input -c copy -avoid_negative_ts 1 $dir_name/clip$i.$file_format" >>run.sh echo "echo \"file 'clip$i.$file_format'\" >>$dir_name/concat.txt" >>run.shdone# if [ $num == 1 ]# then# echo "cp mode"# #echo "cp clip0.mkv ../"\`$output\`"_cat_$file_name" >>run.sh# else# echo "concat mode"# #echo "ffmpeg -f concat -i $dir_name/concat.txt -c copy "\`$output\`"_cat_$file_name" >>run.sh# fi## 如果只提取了单段视频,直接拷贝至放置目录,否则要用ffmpeg合并一次if [ $num == 1 ]then echo "only single clip extract, use cp mode" echo "cp $dir_name/clip0.$file_format \"\`$output\`_cut_$file_name\"" >>run.shelse echo "multi clip extracts, use concat mode" echo "ffmpeg -f concat -i $dir_name/concat.txt -c copy \"\`$output\`_cut_$file_name\"" >>run.shfiecho "rm -rf $dir_name" >>run.shecho "echo script_dir:`pwd`" >>run.shecho "echo -----ok!-----" >>run.shchmod +x run.shecho "-----run.sh has generated!-----"

4.获取视频播放地址

mpv 播放器内置了youtube-dl引擎,可以直接拉到大部分的在线视频流,所以我们只要把浏览器上的地址拿过来就可以,如: mpv 视频网址url, 如果出现 mpv 不支持,播放不了的情况,我们可以使用IDM软件及Downie软件来获取流,同时可以使用you-get annie youtube-dl tampermonkey插件来获取.

#### 方法1:## 借助 IDM Downie4 等工具, 这种方法功能最强大,使用上也最为方便,建议使用#### 方法2: ## 开源及轻量级的方案,很方便批量和定制化,如支持B站列表多集的批量获取及下载you-get -u 视频页浏览器urlannie -i 视频页浏览器urlyoutube-dl -F 视频页浏览器url#### 方法3: ## 使用tampermonkey插件 / FDM / 迅雷等

三. 查看测试效果:

接下来打开mpv播放器来测试一下:

## 终端执行:$ mpv "https://www.bilibili.com/video/BV17B4y1A7Fv" ## B站$ mpv "https://v.youku.com/v_show/id_XNTEzMTkzMzE4NA==.html" ## 优酷$ mpv "~/super_star.mp4" ## 本地文件## 提示:cookies信息可用浏览器调试|fiddler|wireshark查看,或用annie -d查看:$ annie -i -d http://www.bilibili.com/video/av20088587############## 第三方解析地址测试:################ mpv直接播放mpv "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/06/25/326162506/326162506_nb2-1-80.flv?e=ig8euxZM2rNcNbTMhbUVhoMj7wNBhwdEto8g5X10ugNcXBlqNxHxNEVE5XREto8KqJZHUa6m5J0SqE85tZvEuENvNo8g2ENvNo8i8o859r1qXg8xNEVE5XREto8GuFGv2U7SuxI72X6fTr859r1qXg8gNEVE5XREto8z5JZC2X2gkX5L5F1eTX1jkXlsTXHeux_f2o859IB_&uipk=5&nbs=1&deadline=1618889010&gen=playurlv2&os=coso1bv&oi=977184500&trid=b9140b6dd61d4ea382ce3e4d4d5b8cf3u&platform=pc&upsig=b1c7225718f2d5cf81c38165eda223d6&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=456220830&orderid=0,3&agrr=0&logo=80000000" --referrer='https://www.bilibili.com' --no-ytdl## ffmpeg下载ffmpeg -headers $'Referer:http://www.bilibili.com/\r\ny:2\r\n' -user_agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.76 " -ss 3.323 -i https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/06/25/326162506/326162506_nb2-1-80.flv\?e=ig8euxZM2rNcNbTMhbUVhoMj7wNBhwdEto8g5X10ugNcXBlqNxHxNEVE5XREto8KqJZHUa6m5J0SqE85tZvEuENvNo8g2ENvNo8i8o859r1qXg8xNEVE5XREto8GuFGv2U7SuxI72X6fTr859r1qXg8gNEVE5XREto8z5JZC2X2gkX5L5F1eTX1jkXlsTXHeux_f2o859IB_\&uipk=5\&nbs=1\&deadline=1618889010\&gen=playurlv2\&os=coso1bv\&oi=977184500\&trid=b9140b6dd61d4ea382ce3e4d4d5b8cf3u\&platform=pc\&upsig=b1c7225718f2d5cf81c38165eda223d6\&uparams=e\,uipk\,nbs\,deadline\,gen\,os\,oi\,trid\,platform\&mid=456220830\&orderid=0\,3\&agrr=0\&logo=80000000 -t 3.280 .cut_video/clip0.flv

接着我们只要在mpv播放器中通过按键盘字母C键来选定: 开始 ->结束,开始 ->结束多个视频播放时间段即可,确定后按下字母O键,mpv播放器会执行导出.如果要删除并重选,可以按 R 键来操作,其它更多用法请参考上面的GitHub链接.

如下所示: 在终端会实时显示相关的信息,在脚本目录会生成一个run.sh文件,其实原理就是获取了多次按C键选定的时间区域,然后使用了ffmpeg工具来提取了视频.最终再使用ffmpeg进行了合并.

如下图:终端上会显示具体的信息:

mpv处理并生成run.sh文件

Likey@Laptop scripts % pwd /Users/Likey/.config/mpv/scriptsLikey@Laptop scripts % ls README.md c_concat.sh cutter.lua tempCodeRunnerFile.lua test.shautoload.lua c_concat2.sh run.sh test.lua time_pairs.txtLikey@Laptop scripts % cat ./run.sh cd "/Users/Likey/mpv-www-gen"mkdir .cut_videoffmpeg -headers Referer:http://www.bilibili.com/ -user_agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.76 " -ss 1.803 -i http://upos-hz-mirrorakam.akamaized.net/upgcxcode/06/25/326162506/326162506_nb2-1-80.flv\?e=ig8euxZM2rNcNbTMhbUVhoMj7wNBhwdEto8g5X10ugNcXBlqNxHxNEVE5XREto8KqJZHUa6m5J0SqE85tZvEuENvNC8xNEVE9EKE9IMvXBvE2ENvNCImNEVEK9GVqJIwqa80WXIekXRE9IMvXBvEuENvNCImNEVEua6m2jIxux0CkF6s2JZv5x0DQJZY2F8SkXKE9IB5QK==\&deadline=1618939426\&gen=playurl\&nbs=1\&oi=804330220\&os=akam\&platform=pc\&trid=c480ac64f3554ff89735c2db8f551509\&uipk=5\&upsig=50d548dc4f5907be43bc22ae9952cf51\&uparams=e\,deadline\,gen\,nbs\,oi\,os\,platform\,trid\,uipk\&hdnts=exp=1618939426~hmac=7c02aa37e8c2dd3d6045995eba48c2f68667da704f31b5006467d3398f6d2e8a\&mid=0 -t 3.080 .cut_video/clip0.flvecho "file 'clip0.flv'" >>.cut_video/concat.txtffmpeg -headers Referer:http://www.bilibili.com/ -user_agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.76 " -ss 9.963 -i http://upos-hz-mirrorakam.akamaized.net/upgcxcode/06/25/326162506/326162506_nb2-1-80.flv\?e=ig8euxZM2rNcNbTMhbUVhoMj7wNBhwdEto8g5X10ugNcXBlqNxHxNEVE5XREto8KqJZHUa6m5J0SqE85tZvEuENvNC8xNEVE9EKE9IMvXBvE2ENvNCImNEVEK9GVqJIwqa80WXIekXRE9IMvXBvEuENvNCImNEVEua6m2jIxux0CkF6s2JZv5x0DQJZY2F8SkXKE9IB5QK==\&deadline=1618939426\&gen=playurl\&nbs=1\&oi=804330220\&os=akam\&platform=pc\&trid=c480ac64f3554ff89735c2db8f551509\&uipk=5\&upsig=50d548dc4f5907be43bc22ae9952cf51\&uparams=e\,deadline\,gen\,nbs\,oi\,os\,platform\,trid\,uipk\&hdnts=exp=1618939426~hmac=7c02aa37e8c2dd3d6045995eba48c2f68667da704f31b5006467d3398f6d2e8a\&mid=0 -t 6.880 .cut_video/clip1.flvecho "file 'clip1.flv'" >>.cut_video/concat.txtffmpeg -f concat -i .cut_video/concat.txt -c copy "`date "+%Y_%m_%d_%H_%M_%S"`_cut_326162506_nb2-1-80.flv"rm -rf .cut_videoecho ----- ok! -----echo script_dir:/Users/Likey/.config/mpv/scripts

run.sh文件里具体内容

最终生成的文件路径是通过c_concat.sh定义的,你可以按自己的需求改一下,如果不改默认是生成在本地视频文件所在的目录下,但为了方便整体管理和支持网页视频的剪辑,强烈建议你要在磁盘上创建一个目录并设置至以下参数. 我设置的是Users/Likey/mpv-www-gen路径:

打开Users/Likey/mpv-www-gen路径 ,我们看到,视频已经生成了,并可以成功打开.

四. 相关参考资料:

### 分流及分集的处理# flv分集:you-get -p "mpv --referrer='https://www.bilibili.com' --merge-files" https://www.bilibili.com/video/BV1ah41127s7 --playlist# m4s分集:you-get -p "mpv --merge-files" http://www.acfun.cn/v/ac3262317# m3u8线上流拉取:you-get -u https://www.acfun.cn/v/ac3262317 -playlists # 其它有价值的参考https://github.com/mpv-player/mpv/issues/1178https://stackoverflow.com/questions/49343174/can-ffmpeg-concatenate-files-from-a-different-domainhttps://superuser.com/questions/692990/use-ffmpeg-copy-codec-to-combine-ts-files-into-a-single-mp4

感谢以上的作者! 本文结束...


打赏

本文链接:https://www.kinber.cn/post/3905.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客