ffmpeg导出视频不能播放,改成opencv导出成功

from glob import glob
import json
import os
import cv2
import subprocess
from tqdm import tqdm
from cv2 import (CAP_PROP_FOURCC, CAP_PROP_FPS, CAP_PROP_FRAME_COUNT,
                 CAP_PROP_FRAME_HEIGHT, CAP_PROP_FRAME_WIDTH,
                 CAP_PROP_POS_FRAMES, VideoWriter_fourcc)

def get_color(i):
    return [(i * 23 * j + 43) % 255 for j in range(3)]

# with open("../data/Dataset/mot/det_db_motrv2.json") as f:
#     det_db = json.load(f)

with open("../data/Dataset/mot/det_dancetrack_val.json") as f:
    det_db = json.load(f)
def process(trk_path, img_list, output="output.mp4"):
    h, w, _ = cv2.imread(img_list[0]).shape
    command = [
        "/usr/bin/ffmpeg",
        '-y',  # overwrite output file if it exists
        '-f', 'rawvideo',
        '-vcodec','rawvideo',
        '-s', f'{w}x{h}',  # size of one frame
        '-pix_fmt', 'bgr24',
        '-r', '20',  # frames per second
        '-i', '-',  # The imput comes from a pipe
        '-vcodec', 'libx264',
        '-s', f'{w//2*2}x{h//2*2}',
        '-an',  # Tells FFMPEG not to expect any audio
        '-loglevel', 'error',
        #'-strict', '- 2',
        '-crf', '26',
        '-pix_fmt', 'yuv420p',
        '-f','mp4'
        # '-preset', 'ultrafast',
        # '-c','copy'
    ]
    #writing_process = subprocess.Popen(command + [output], stdin=subprocess.PIPE)
    vwriter = cv2.VideoWriter(output, VideoWriter_fourcc(*'XVID'), 30,
                              (w, h))
    # tracklets = defaultdict(list)
    # for line in open(trk_path):
    #     t, id, *xywhs = line.split(',')[:7]
    #     t, id = map(int, (t, id))
    #     x, y, w, h, s = map(float, xywhs)
    #     tracklets[t].append((id, *map(int, (x, y, x+w, y+h))))

    for i, path in enumerate(tqdm(sorted(img_list))):
        im = cv2.imread(path)
        #for det in det_db[path.replace('.jpg', '.txt')[20:]]:
        for det in det_db[path.replace('.jpg', '.txt')[35:]]:
            x1, y1, w, h, _ = map(int, map(float, det.strip().split(',')))
            im = cv2.rectangle(im, (x1, y1), (x1+w, y1+h), (255, 255, 255), 6)
        # for j, x1, y1, x2, y2 in tracklets[i + 1]:
        #     im = cv2.rectangle(im, (x1, y1), (x2, y2), get_color(j), 4)
        #     im = cv2.putText(im, f"{j}", (x1 + 10, y1 + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, get_color(j), 2)
        vwriter.write(im)
    vwriter.release()
        #writing_process.stdin.write(im.tobytes())
        # try:
        #     writing_process.stdin.write(im.tobytes())
        # except Exception:
        #     pass
    # writing_process.stdin.close()
    # writing_process.wait()

if __name__ == '__main__':
    #jobs = os.listdir("exps/motrv2_noqd/run1/tracker/")
    jobs = os.listdir("../tracker")
    rank = int(os.environ.get('RLAUNCH_REPLICA', '0'))
    ws = int(os.environ.get('RLAUNCH_REPLICA_TOTAL', '1'))
    jobs = sorted(jobs)[rank::ws]
    for seq in jobs:
        print(seq)

        #trk_path = "exps/motrv2_noqd/run1/tracker/" + seq
        trk_path = "../tracker/" + seq
        # trk_path = "/data/Dataset/mot/DanceTrack/val/dancetrack0010/gt/gt.txt"

        #img_list = glob(f"../data/Dataset/mot/DanceTrack/test/{seq[:-4]}/img1/*.jpg")
        img_list = glob(f"../data/Dataset/mot/DanceTrack/val/dancetrack0010/img1/*.jpg")
        #process(trk_path, img_list, f'../demo_vid/{seq[:-4]}.mp4')
        process(trk_path, img_list, f'../demo_vid/dancetrack0010.mp4')
        break

代码改自MOTRv2/visualize.py at main · megvii-research/MOTRv2 · GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值