Python-如何将视频文件读取为二进制数据? [英] Python - How to read a video file as binary data?

查看:1348
本文介绍了Python-如何将视频文件读取为二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的作业要求我从视频文件计算哈希值.因此,我猜测我需要做的是以某种方式将视频文件读取为二进制数据,然后使用该方法进行哈希运算.问题是,我只知道如何读写.txt文件-视频文件对我来说是全新的.所以我的问题是:

My assignment asks me to compute a hash from a video file. So I'm guessing what I need to do, is to somehow read the video file as binary data, and then do the hashing shenanigans with that. The problem is, I only know how to read from and write to .txt files - video files are completely new to me. So my questions are:

如何获取文件并将其读取为二进制数据?

How do I take a file and read it as binary data?

如何处理这些数据?我的意思是,我应该将其粘贴到字符串中还是应该使用某种数组?我想数字的数量将是巨大的,并且我不希望计算机崩溃,因为我以某种极其低效的方式处理数据:D.

How do I handle this data? I mean, should I just stick it into a string or should I use an array of some sort? I imagine the amount of numbers is going to be huge and I wouldn't like my computer to crash because I handled the data in some horribly inefficient way :D.

此外,我不太确定我在说二进制数据"时在说什么,因为我对这类东西的经验有限.我的意思是,它不仅是一串1和0,对吗?因此,我也非常感谢有关二进制数据"的速成课程:D

Also, I am not entirely sure what I am talking about when I say "binary data", as I have limited experience with that kind of stuff. I mean, it's not just a string of 1s and 0s right?. So I would also appreciate a crash course on "binary data" :D

推荐答案

文本数据和二进制数据之间确实没有区别.字符串只是字节序列.每个字节或几个字节的值都对应一个文本字符.因此,我们可以像读取字符串一样读取和存储二进制数据(字节序列).唯一的区别是,我们从二进制文件中读取的字符序列可能不会被人类读取.

There is really no difference between text data and binary data. A string is just a sequence of bytes. Every byte, or several byte, values corresponds to a text character. Because of this we can read and store binary data (a sequence of bytes) just like a string. The only difference is that the sequence of characters we read from binary will probably not be readable by humans.

使用"rb"(读取二进制)标记文件打开格式,以避免出现文本行结尾问题. 要处理大文件,您可以一次读取少量字节,然后随便计算字节的哈希值.

Mark the file open format with "rb" (read binary) to avoid text line ending problems. To handle large files, you could read a small number of bytes at a time and compute the hash of the bytes as you go.

started = 0
hash_val = 0

with open("video", "rb") as file:
    byte = file.read(1) # read a byte (a single character in text)
    byte_val = ord(byte) # convert the string character into a number

    if started == 0:
        hash_val = byte_val
        started = 1
    hash_val = (hash_val << 5) - hash_val + byte_val # this is a basic hash

print(hash_val)

这篇关于Python-如何将视频文件读取为二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆