Python语言,ffmpeg查看音视频文件信息,Base64文本转音视频文件

ffmpeg的ffprobe查看音频视频文件信息

功能:查看某一个未知的音视频文件,输出JSON格式的信息,获知音视频格式等信息。

代码实现1:

import subprocess
import json
path = r'D:\001.mp3'
command = (r'ffprobe -print_format json -loglevel quiet -show_streams %s'%path)
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = result.stdout.read()
temp = str(out.decode('utf-8'))
print("执行命令:" + command)
# print(json.loads(temp))
text = json.loads(temp)
print(json.dumps(text, indent=2,ensure_ascii=False))

代码实现2:

import subprocess
import json
path = r'D:\001.mp3'
command = ["ffprobe.exe", "-loglevel", "quiet", "-print_format", "json", "-show_format", "-show_streams", "-i", path]
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = result.stdout.read()
temp = str(out.decode('utf-8'))
# print(json.loads(temp))
text = json.loads(temp)
print(json.dumps(text, indent=2,ensure_ascii=False))

执行后输出:

执行命令:ffprobe -print_format json -loglevel quiet -show_streams D:\001.mp3
{
  "streams": [
    {
      "index": 0,
      "codec_name": "mp3",
      "codec_long_name": "MP3 (MPEG audio layer 3)",
      "codec_type": "audio",
      "codec_tag_string": "[0][0][0][0]",
      "codec_tag": "0x0000",
      "sample_fmt": "fltp",
      "sample_rate": "16000",
      "channels": 1,
      "channel_layout": "mono",
      "bits_per_sample": 0,
      "r_frame_rate": "0/0",
      "avg_frame_rate": "0/0",
      "time_base": "1/14112000",
      "start_pts": 0,
      "start_time": "0.000000",
      "duration_ts": 382548096,
      "duration": "27.108000",
      "bit_rate": "16000",
      "disposition": {
        "default": 0,
        "dub": 0,
        "original": 0,
        "comment": 0,
        "lyrics": 0,
        "karaoke": 0,
        "forced": 0,
        "hearing_impaired": 0,
        "visual_impaired": 0,
        "clean_effects": 0,
        "attached_pic": 0,
        "timed_thumbnails": 0,
        "captions": 0,
        "descriptions": 0,
        "metadata": 0,
        "dependent": 0,
        "still_image": 0
      }
    }
  ]
}

Base64文本转音视频文件

功能:HTTP传输,content-type中audio/x-mpeg,mpeg格式是一种视频格式,自定义接口时,通常选择将mpeg格式的数据进行base64。如下功能是将base64后的文本转为视频文件。

代码实现

import base64
#读取文本文件
readFile = open("d://001.txt", 'r', encoding='utf-8')
#读取全部内容 ,并以列表方式返回
lines = readFile.readlines()
readFile.close()
#列表转为字符串
text=''.join(lines)
print(text)
bts=base64.b64decode(text)
#base64解码
print(type(bts))
#写入的文件(字节)
writeFile = open("d://111.mp3",'wb+')
writeFile.write(bts)
writeFile.close()
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章