最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python如何實(shí)現(xiàn)視頻轉(zhuǎn)代碼視頻

 更新時(shí)間:2019年06月17日 15:31:10   作者:丿灬安之若死  
這篇文章主要為大家詳細(xì)介紹了python如何實(shí)現(xiàn)視頻轉(zhuǎn)代碼視頻,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python如何實(shí)現(xiàn)視頻轉(zhuǎn)代碼視頻的具體代碼,供大家參考,具體內(nèi)容如下

# -*- coding:utf-8 -*-
#coding:utf-8
import argparse
import os
import cv2
import subprocess
from cv2 import VideoWriter, VideoWriter_fourcc, imread, resize
from PIL import Image, ImageFont, ImageDraw
 
# 命令行輸入?yún)?shù)處理
# aparser = argparse.ArgumentParser()
# aparser.add_argument('file')
# aparser.add_argument('-o','--output')
# aparser.add_argument('-f','--fps',type = float, default = 24)#幀
# aparser.add_argument('-s','--save',type = bool, nargs='?', default = False, const = True)
# 是否保留Cache文件,默認(rèn)不保存
 
# 獲取參數(shù)
# args = parser.parse_args()
# INPUT = args.file
# OUTPUT = args.output
# SAVE = args.save
# FPS = args.fps
# 像素對(duì)應(yīng)ascii碼
 
 
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:oa+>!:+. ")
 
 
# ascii_char = list("MNHQ$OC67+>!:-. ")
# ascii_char = list("MNHQ$OC67)oa+>!:+. ")
 
# 將像素轉(zhuǎn)換為ascii碼
def get_char(r, g, b, alpha=256):
  if alpha == 0:
    return ''
  length = len(ascii_char)
  gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
  unit = (256.0 + 1) / length
  return ascii_char[int(gray / unit)]
 
 
# 將txt轉(zhuǎn)換為圖片
def txt2image(file_name):
  im = Image.open(file_name).convert('RGB')
  # gif拆分后的圖像,需要轉(zhuǎn)換,否則報(bào)錯(cuò),由于gif分割后保存的是索引顏色
  raw_width = im.width
  raw_height = im.height
  width = int(raw_width / 6)
  height = int(raw_height / 15)
  im = im.resize((width, height), Image.NEAREST)
 
  txt = ""
  colors = []
  for i in range(height):
    for j in range(width):
      pixel = im.getpixel((j, i))
      colors.append((pixel[0], pixel[1], pixel[2]))
      if (len(pixel) == 4):
        txt += get_char(pixel[0], pixel[1], pixel[2], pixel[3])
      else:
        txt += get_char(pixel[0], pixel[1], pixel[2])
    txt += '\n'
    colors.append((255, 255, 255))
 
  im_txt = Image.new("RGB", (raw_width, raw_height), (255, 255, 255))
  dr = ImageDraw.Draw(im_txt)
  # font = ImageFont.truetype(os.path.join("fonts","漢儀楷體簡(jiǎn).ttf"),18)
  font = ImageFont.load_default().font
  x = y = 0
  # 獲取字體的寬高
  font_w, font_h = font.getsize(txt[1])
  font_h *= 1.37 # 調(diào)整后更佳
  # ImageDraw為每個(gè)ascii碼進(jìn)行上色
  for i in range(len(txt)):
    if (txt[i] == '\n'):
      x += font_h
      y = -font_w
      # self, xy, text, fill = None, font = None, anchor = None,
      #*args, ** kwargs
    dr.text((y, x), txt[i], fill=colors[i])
    #dr.text((y, x), txt[i], font=font, fill=colors[i])
    y += font_w
 
  name = file_name
  #print(name + ' changed')
  im_txt.save(name)
 
 
# 將視頻拆分成圖片
def video2txt_jpg(file_name):
  vc = cv2.VideoCapture(file_name)
  c = 1
  if vc.isOpened():
    r, frame = vc.read()
    if not os.path.exists('Cache'):
      os.mkdir('Cache')
    os.chdir('Cache')
  else:
    r = False
  while r:
    cv2.imwrite(str(c) + '.jpg', frame)
    txt2image(str(c) + '.jpg') # 同時(shí)轉(zhuǎn)換為ascii圖
    r, frame = vc.read()
    c += 1
  os.chdir('..')
  return vc
 
 
# 將圖片合成視頻
def jpg2video(outfile_name, fps):
  fourcc = VideoWriter_fourcc(*"MJPG")
 
  images = os.listdir('Cache')
  im = Image.open('Cache/' + images[0])
  vw = cv2.VideoWriter(outfile_name + '.avi', fourcc, fps, im.size)
 
  os.chdir('Cache')
  for image in range(len(images)):
    # Image.open(str(image)+'.jpg').convert("RGB").save(str(image)+'.jpg')
    frame = cv2.imread(str(image + 1) + '.jpg')
    vw.write(frame)
    #print(str(image + 1) + '.jpg' + ' finished')
  os.chdir('..')
  vw.release()
 
 
# 遞歸刪除目錄
def remove_dir(path):
  if os.path.exists(path):
    if os.path.isdir(path):
      dirs = os.listdir(path)
      for d in dirs:
        if os.path.isdir(path + '/' + d):
          remove_dir(path + '/' + d)
        elif os.path.isfile(path + '/' + d):
          os.remove(path + '/' + d)
      os.rmdir(path)
      return
    elif os.path.isfile(path):
      os.remove(path)
    return
 
 
# 調(diào)用ffmpeg獲取mp3音頻文件
def video2mp3(file_name):
  outfile_name = file_name.split('.')[0] + '.mp3'
  subprocess.call('ffmpeg -i ' + file_name + ' -f mp3 ' + outfile_name, shell=True)
 
 
# 合成音頻和視頻文件
def video_add_mp3(file_name, mp3_file):
  outfile_name = file_name.split('.')[0] + '-txt.mp4'
  subprocess.call('ffmpeg -i ' + file_name + ' -i ' + mp3_file + ' -strict -2 -f mp4 ' + outfile_name, shell=True)
 
 
if __name__ == '__main__':
  INPUT = r"G:\py\學(xué)習(xí)python\視頻到代碼\video39.mp4"
  OUTPUT = r"G:\py\學(xué)習(xí)python\視頻到代碼\video39_2.mp4"
  SAVE = r"G:\py\學(xué)習(xí)python\視頻到代碼\\video39_3"
  FPS = "24"
  vc = video2txt_jpg(INPUT)
  FPS = vc.get(cv2.CAP_PROP_FPS) # 獲取幀率
  print(FPS)
 
  vc.release()
 
  jpg2video(INPUT.split('.')[0], FPS)
  print(INPUT, INPUT.split('.')[0] + '.mp3')
  video2mp3(INPUT)
  video_add_mp3(INPUT.split('.')[0] + '.avi', INPUT.split('.')[0] + '.mp3')
 
  if (not SAVE):
    remove_dir("Cache")
    os.remove(INPUT.split('.')[0] + '.mp3')
    os.remove(INPUT.split('.')[0] + '.avi')

流程圖:

這次python編程的流程圖如下: 


注意事項(xiàng):

在編程的過程中有需要注意的幾點(diǎn):

  • 這次編程使用到了opencv庫(kù),需要安裝
  • 幀率的獲取可以通過這個(gè)函數(shù)——FPS = vc.get(cv2.CAP_PROP_FPS)
  • 合成后的視頻是沒有聲音的,我們使用ffmpeg進(jìn)行合成

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python如何讀取相對(duì)路徑文件

    Python如何讀取相對(duì)路徑文件

    這篇文章主要介紹了Python如何讀取相對(duì)路徑文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • python pycharm的安裝及其使用

    python pycharm的安裝及其使用

    這篇文章主要介紹了python pycharm的安裝及其使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python使用多線程+socket實(shí)現(xiàn)端口掃描

    python使用多線程+socket實(shí)現(xiàn)端口掃描

    這篇文章主要為大家詳細(xì)介紹了python使用多線程+socket實(shí)現(xiàn)端口掃描,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Python自定義一個(gè)類實(shí)現(xiàn)字典dict功能的方法

    Python自定義一個(gè)類實(shí)現(xiàn)字典dict功能的方法

    今天小編就為大家分享一篇Python自定義一個(gè)類實(shí)現(xiàn)字典dict功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python數(shù)學(xué)符號(hào)計(jì)算庫(kù)SymPy使用方法詳解

    Python數(shù)學(xué)符號(hào)計(jì)算庫(kù)SymPy使用方法詳解

    SymPy?是一個(gè)?Python?的數(shù)學(xué)符號(hào)計(jì)算庫(kù),提供了強(qiáng)大的工具來進(jìn)行符號(hào)數(shù)學(xué)運(yùn)算、代數(shù)操作、求解方程、微積分、矩陣運(yùn)算等,它廣泛應(yīng)用于數(shù)學(xué)教學(xué)、物理學(xué)、工程學(xué)、統(tǒng)計(jì)學(xué)和概率論等領(lǐng)域,本文將結(jié)合具體案例,詳細(xì)介紹?SymPy?的使用方法,需要的朋友可以參考下
    2024-08-08
  • python模塊之sys模塊和序列化模塊(實(shí)例講解)

    python模塊之sys模塊和序列化模塊(實(shí)例講解)

    下面小編就為大家?guī)硪黄猵ython模塊之sys模塊和序列化模塊(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • python進(jìn)行二次方程式計(jì)算的實(shí)例講解

    python進(jìn)行二次方程式計(jì)算的實(shí)例講解

    在本篇內(nèi)容里小編給大家整理了一篇關(guān)于python進(jìn)行二次方程式計(jì)算的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-12-12
  • Python報(bào)錯(cuò)too?many?values?to?unpack問題及解決

    Python報(bào)錯(cuò)too?many?values?to?unpack問題及解決

    這篇文章主要介紹了Python報(bào)錯(cuò)too?many?values?to?unpack問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 布同自制Python函數(shù)幫助查詢小工具

    布同自制Python函數(shù)幫助查詢小工具

    Python的自帶文檔功能很不錯(cuò),給與我耳目一新的感覺。如果在CMD中進(jìn)行“行式編程”編程的話,可以隨時(shí)隨地的查詢函數(shù)的功能,非常方便。
    2011-03-03
  • Python數(shù)據(jù)可視化編程通過Matplotlib創(chuàng)建散點(diǎn)圖代碼示例

    Python數(shù)據(jù)可視化編程通過Matplotlib創(chuàng)建散點(diǎn)圖代碼示例

    這篇文章主要介紹了Python數(shù)據(jù)可視化編程通過Matplotlib創(chuàng)建散點(diǎn)圖實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12

最新評(píng)論

伊宁市| 承德县| 石景山区| 文昌市| 乌兰浩特市| 化州市| 大同市| 长岭县| 潼南县| 同德县| 吉水县| 长岛县| 涿鹿县| 怀来县| 保定市| 大关县| 淳安县| 华池县| 洪洞县| 玉屏| 邮箱| 衡阳市| 遂溪县| 泸西县| 和硕县| 陕西省| 平山县| 璧山县| 赞皇县| 盐源县| 若尔盖县| 青神县| 安平县| 昌宁县| 凌云县| 常熟市| 江油市| 金湖县| 文昌市| 钟山县| 辉县市|