• youtube-dl 进程间通信实战


    从命令行获取视频信息, 输出如下

    [Log] -> 执行命令'youtube-dl https://youtu.be/sbz3fOe7rog -F'结果:
    [youtube] sbz3fOe7rog: Downloading webpage
    [youtube] sbz3fOe7rog: Downloading video info webpage
    [info] Available formats for sbz3fOe7rog:
    format code  extension  resolution note
    249          webm       audio only tiny   59k , opus @ 50k (48000Hz), 1.50MiB
    250          webm       audio only tiny   78k , opus @ 70k (48000Hz), 2.00MiB
    140          m4a        audio only tiny  129k , m4a_dash container, mp4a.40.2@128k (44100Hz), 3.47MiB
    251          webm       audio only tiny  150k , opus @160k (48000Hz), 3.85MiB
    278          webm       256x144    144p   95k , webm container, vp9, 15fps, video only, 2.36MiB
    160          mp4        256x144    144p  111k , avc1.4d400c, 15fps, video only, 2.95MiB
    242          webm       426x240    240p  162k , vp9, 15fps, video only, 2.62MiB
    133          mp4        426x240    240p  247k , avc1.4d4015, 15fps, video only, 6.58MiB
    18           mp4        512x288    240p  355k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 9.58MiB (best)
    
    

    此时 echo -n $? 返回 0

    使用正则表达式来匹配, 分组并捕获数据

    			val listAudio = ArrayList<Audio>()
    			val listVideo = ArrayList<Video>()
    
    			for (a in output.split('
    ')) {
    				val videoRegex = """^(d+)s+(w+)s+(d+xd+)s+(d+)ps+(d+)k , (.*), video only, (.+)MiB$""".toRegex()
    						var mr = videoRegex.matchEntire(a)
    						if (mr != null){
    							val (id2, format, scale, frame, rate, info, size) = mr.destructured
    							val e = Video(id2.toInt(), format, scale, frame.toInt(), rate.toInt(), info, size.toDouble())
    							println(e)
    							listVideo.add(e)
    							continue
    						}
    
    				val audioRegex = """^(d+)s+(w+)s+audio only tinys+(d+)k , (.*),s+(.+)MiB$""".toRegex()
    						mr = audioRegex.matchEntire(a)
    						if (mr != null) {
    							val (id2, format, rate, info, size) = mr.destructured
    							val e = Audio(id2.toInt(), format, rate.toInt(), info, size.toDouble())
    							println(e)
    							listAudio.add(e)
    							continue
    						}
    
    				val videoRegex2 = """^(d+)s+(w+)s+(d+xd+)s+(d+)ps+(d+)k , (.*), (.+)MiB.+best.+$""".toRegex()
    						mr = videoRegex2.matchEntire(a)
    						if (mr != null){
    							val (id2, format, scale, frame, rate, info, size) = mr.destructured
    							val e = Video(id2.toInt(), format, scale, frame.toInt(), rate.toInt(), info, size.toDouble())
    							global.log("best video -> $e")
    							listVideo.add(e)
    							continue
    						}
    			}
    
    			return mapOf(
    				"v" to id,
    
    				"best"  to mapOf(
    					"audio" to listAudio.maxBy{ it.rate },
    					"video" to listVideo.maxBy{ it.rate }
    				),
    				
    				"available" to mapOf(
    					"audios" to listAudio,
    					"videos" to listVideo
    				)
    			)
    
    

    一个接口就诞生了

    {
      "v" : "Ce-4lXZxmvM",
      "best" : {
        "audio" : {
          "id" : 251,
          "format" : "webm",
          "rate" : 164,
          "info" : "opus @160k (48000Hz)",
          "size" : 31.89
        },
        "video" : {
          "id" : 137,
          "format" : "mp4",
          "scale" : "1440x1080",
          "frame" : 1080,
          "rate" : 1643,
          "info" : "avc1.640028, 30fps",
          "size" : 30.17
        }
      },
      "available" : {
        "audios" : [ {
          "id" : 249,
          "format" : "webm",
          "rate" : 73,
          "info" : "opus @ 50k (48000Hz)",
          "size" : 12.1
        }, {
          "id" : 250,
          "format" : "webm",
          "rate" : 93,
          "info" : "opus @ 70k (48000Hz)",
          "size" : 16.1
        }, {
          "id" : 140,
          "format" : "m4a",
          "rate" : 132,
          "info" : "m4a_dash container, mp4a.40.2@128k (44100Hz)",
          "size" : 31.22
        }, {
          "id" : 251,
          "format" : "webm",
          "rate" : 164,
          "info" : "opus @160k (48000Hz)",
          "size" : 31.89
        } ],
        "videos" : [ {
          "id" : 278,
          "format" : "webm",
          "scale" : "192x144",
          "frame" : 144,
          "rate" : 59,
          "info" : "webm container, vp9, 30fps",
          "size" : 6.39
        }, {
          "id" : 160,
          "format" : "mp4",
          "scale" : "192x144",
          "frame" : 144,
          "rate" : 84,
          "info" : "avc1.4d400c, 30fps",
          "size" : 2.87
        }, {
          "id" : 242,
          "format" : "webm",
          "scale" : "320x240",
          "frame" : 240,
          "rate" : 117,
          "info" : "vp9, 30fps",
          "size" : 10.15
        }, {
          "id" : 133,
          "format" : "mp4",
          "scale" : "320x240",
          "frame" : 240,
          "rate" : 175,
          "info" : "avc1.4d400d, 30fps",
          "size" : 4.22
        }, {
          "id" : 243,
          "format" : "webm",
          "scale" : "480x360",
          "frame" : 360,
          "rate" : 226,
          "info" : "vp9, 30fps",
          "size" : 16.66
        }, {
          "id" : 244,
          "format" : "webm",
          "scale" : "640x480",
          "frame" : 480,
          "rate" : 378,
          "info" : "vp9, 30fps",
          "size" : 25.6
        }, {
          "id" : 134,
          "format" : "mp4",
          "scale" : "480x360",
          "frame" : 360,
          "rate" : 416,
          "info" : "avc1.4d401e, 30fps",
          "size" : 7.99
        }, {
          "id" : 247,
          "format" : "webm",
          "scale" : "960x720",
          "frame" : 720,
          "rate" : 641,
          "info" : "vp9, 30fps",
          "size" : 42.0
        }, {
          "id" : 135,
          "format" : "mp4",
          "scale" : "640x480",
          "frame" : 480,
          "rate" : 728,
          "info" : "avc1.4d401e, 30fps",
          "size" : 13.01
        }, {
          "id" : 248,
          "format" : "webm",
          "scale" : "1440x1080",
          "frame" : 1080,
          "rate" : 1039,
          "info" : "vp9, 30fps",
          "size" : 69.33
        }, {
          "id" : 136,
          "format" : "mp4",
          "scale" : "960x720",
          "frame" : 720,
          "rate" : 1129,
          "info" : "avc1.4d401f, 30fps",
          "size" : 20.92
        }, {
          "id" : 137,
          "format" : "mp4",
          "scale" : "1440x1080",
          "frame" : 1080,
          "rate" : 1643,
          "info" : "avc1.640028, 30fps",
          "size" : 30.17
        }, {
          "id" : 18,
          "format" : "mp4",
          "scale" : "480x360",
          "frame" : 360,
          "rate" : 275,
          "info" : "avc1.42001E, mp4a.40.2@ 96k (44100Hz)",
          "size" : 66.41
        } ]
      }
    }
    

  • 相关阅读:
    ThinkPHP3.2 整合支付宝RSA加密方式
    代码风格规范
    Mac下安装composer
    MAC 下安装RabbitMQ
    Redis配置
    git 分支
    PHP常用数组操作方法汇总
    php 不用第三个变量 交换两个变量的值汇总
    PHP配置错误信息回报的等级
    Apache同一个IP上配置多域名
  • 原文地址:https://www.cnblogs.com/develon/p/11679592.html
Copyright © 2020-2023  润新知