shell命令行操作录制并自动进行关键内容截图(2)

该博客讲述了如何利用日志文件来安排视频截图的时间点,并通过Python脚本计算时间差,生成ffmpeg命令进行截图。内容涉及日志解析、时间处理及文件命名,为视频处理提供了自动化方案。

当视频准备好之后,就需要进行截图了。
截图的方式,需要日志文件以特定的方式进行排列。这样可以计算出需要截取的时间,以及输出的文件名。

输入文件类似下面这样:

start_record 2022-07-10_08:00:00
pwd
ls
ll -h
ls
ll /opt/
ls -l
echo "helloworld"
echo "shell"
grep "ERROR" /var/log/messages
time ls
systemctl status redis
pwd
ls
ll -h
ls
ll /opt/
ls -l
echo "helloworld"
echo "shell"
grep "ERROR" /var/log/messages
time ls
systemctl status redis
systemctl status mysqld
systemctl status mysqld
end_record 2022-07-10_10:00:00 1.1.1_列出当前路径
crontab -l
end_record 2022-07-10_10:01:20 1.1.2_查看crontab的定时任务
systemctl status sshd
end_record 2022-07-10_12:02:30 1.1.3_查看sshd服务

当日志以这种方式进行记录之后,就可以使用如下方式进行数据处理。

import datetime
# need_record_dict key是时间差,name是要命名的文件 ,输出craw.sh 文件, 执行即可
need_record_dict = {}
with open("process.log", 'r', encoding="utf-8") as fr:
    for index, line in enumerate(fr):
        line = line.strip()
        if line.startswith("start_record"):
            basetime = line.split(" ")[1]
            start_time = datetime.datetime.strptime(basetime ,"%Y-%m-%d_%H:%M:%S")

        elif line.startswith("end_record"):
            base, recordtime, name = line.split(" ")
            end_time = datetime.datetime.strptime(recordtime ,"%Y-%m-%d_%H:%M:%S")
            timediff = (end_time - start_time).seconds
            hour = str(timediff // 3600).zfill(2)
            minute = str((timediff-int(hour)*3600) // 60).zfill(2)
            second = str(timediff-int(hour)*3600-int(minute)*60).zfill(2)
            outStr = f'{hour}:{minute}:{second}'
            
            need_record_dict[outStr] = name
    
need_craw_file="craw.sh"
with open(need_craw_file, "w", encoding="utf-8") as ff:
    for k,v in need_record_dict.items():
        ff.write(f"ffmpeg -i output.gif -ss {k} -vframes 1 {v}.png\n")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值