Gps与时间信息的提取

大家在跟踪定位的系统中,可能碰到这样的问题:视频采集之后形成的文件,在此文件中包括 gps数据信息,然后将根据该文件中的gps信息提取出来,然后然后根据这些信息在平面上将轨迹绘制出来,在这里,我主要讲解 mpeg文件的基本格式以及gps信息的提取,关于绘制,将gps信息转换的坐标的问题,读者可以自己查阅相关的资料

 

先看看文件序列头饰如何的定义 ,我也是从大网上搜索到的,不再进行翻译,我想对于任何一个开发者来说这点英文不算什么,你自己翻译。

 sequence header

 

this contains information related to one or more "group-of-pictures"

 

byte# data details

==================================================================

1-4 sequence header in hex 000001b3

code

12 bits horizontal size in pixels

12 bits vertical size in pixels

4 bits pel aspect ratio see below

18 bits picture rate see below

1 bit marker bit always 1

10 bits vbv buffer size minimum buffer needed to decode this

sequence of pictures; in 16kb units

1 bit constrained

parameter flag

1 bit load intra 0: false; 1: true (matrix follows)

quantizer matrix

64 bytes intra quantizer optional

matrix

1 bit load nonintra 0: false; 1: true (matrix follows)

quantizer matrix

64 bytes nonintra quantizer optional

matrix

- squence extension optional

data

- user data optional application-dependent data

===================================================================

 

aspect raios are defined by a code which represents the height and

width of the video image.

picture rates are also defined by a code that represents the number

of pictures that may be displayed each second.

 

each group of pictures has a header that contains one "i picture"

and zero or more b and p pictures. the header is concerned with

the time synchronisation for the first picture in this group, and

the closeness of the previous group to this one.

 

/*****************************************************************/

 

for picture rate:

1 = 23.976 frames/sec

2 = 24

3 = 25

4 = 29.97

5 = 30

6 = 50

7 = 59.94

8 = 60

 

here gives an example. below is hex dump of first 256 bytes of

the first video frame of test.mpg from xingmpeg.

 

00 00 01 b3 16 00 f 0 c 4 02 a 3 20 a 5 10 12 12 14

14 14 16 16 16 16 18 18 19 18 18 1a 1b 1b 1b 1b

1a 1c 1d 1e 1e 1e 1d 1c 1e 1f 20 21 21 20 1f 1e

21 23 23 24 23 23 21 25 26 27 27 26 25 29 2a 2a

2a 29 2d 2d 2d 2d 30 31 30 34 34 38 16 00 f 0 c 4

00 00 01 b8 00 08 00 00 00 00 01 00 00 0a 72 00

00 00 01 01 13 f 9 50 02 bc b2 b8 be 68 8b a4 9f

c5 b5 ca 00 56 76 39 65 f 2 30 8b a6 9d 50 69 e7

da fe 13 cf b7 ff 8f f4 ce 7b fa 0e f0 66 ae 1c

5d e7 00 c 8 0a 92 b9 29 3c 21 23 f 1 d6 40 13 06

f0 10 10 c 6 27 80 a 0 34 e 1 c 8 e4 0f 74 91 da c4

03 a 0 dc 03 12 60 18 49 27 1d d4 bc 67 0e 54 8c

96 fc 5d c0 06 e0 1a 72 11 7c 9a 8d c9 45 89 6d

cd c4 0b 63 dc 90 18 24 00 ec 84 90 18 10 c 9 3b

1e a7 60 3c 9d 74 80 76 05 0b 02 81 a 9 29 39 68

53 8f 59 f 1 bf 93 fb a0 04 01 bc b0 ce 18 e1 25

 

sequence header = (hex) 00 00 01 b3

horizontal size = 0x160 = 352

vertical size = 0x 0f 0 = 240

pel aspect ratio = [i don't know]

picture rate = 4 = 29.97 frames/sec

marker bit = 1

 

如果你连这个都看不懂,那建议你不用往下看,因为下面连这样的清楚的讲解都没有。当我们知道了文件的头以及连续动画图片的数据信息,(在例子代码中有视频与音频数据的分解提取)。

 

理解上面的上面头文件格式之后,让我们来实现测定 MPEG Stream Type

 

关于DWORD_SWAP NextStartCode 在源码中都有,你只要明白这么做,具体为什么这么做,似乎也是你自己该研究的问题,或许你有更好的方法,抛砖引玉 (这样你会更深刻的理解与操作mpeg文件)。

 

int CheckMPEGStreamType (const char * mpgfile)

{

     char sDataBuf [32768];

     char *sPtr;

     DWORD dwLeft, dwPackStartCode;

     FILE * fp;

     int iRet = -1;

     BOOL bMP4 = false;

 

     fp = fopen (mpgfile, "rb");

     if (!fp)

         return iRet;

     dwLeft = fread (sDataBuf, 1, 32768, fp);

     fclose (fp);

 

     sPtr = sDataBuf;

    

     if (DWORD_SWAP(*(UNALIGNED DWORD *)sPtr) == 0x000001BA)

     {     // MPEG-1 Syatem, MPEG-2 program or MPEG-4 Program

         // check if MPEG-1 or MPEG-2(MPEG-4)

         if ((*(PBYTE)(sPtr+4) & 0x40) == 0x0) {         // MPEG-1 Syatem

              iRet = 1;

         } else { // Check if MPEG-2 or MPEG-4

              bMP4 = false;

              while (dwLeft >= 4)

{

                   NextStartCode ((const BYTE **)&sPtr, &dwLeft);

                   dwPackStartCode = DWORD_SWAP(*(UNALIGNED DWORD *)sPtr);

                   if (dwPackStartCode == 0x000001b6) { // MPEG-4 stuff

                       bMP4 = true;

                       break;

                   }

                   dwLeft --;

                   sPtr ++;

              }

              if (bMP4)

                   iRet = 4;

              else

                   iRet = 2;

         }

     } else if (*sPtr == 0x47)

 {                // MPEG-2 Transport or MPEG-4 Transport

         while (dwLeft >= 188 && *(char *)sPtr == 0x47)

{

              DWORD dwSubLeft;

              char * sSubPtr;

              dwSubLeft = dwLeft;

              sSubPtr = sPtr;

              while (dwSubLeft >= 4) {

                   NextStartCode ((const BYTE **)&sSubPtr, &dwSubLeft);

                   dwPackStartCode = DWORD_SWAP(*(UNALIGNED DWORD *)sSubPtr);

                   if (dwPackStartCode == 0x000001b6) { // MPEG-4 stuff

                       bMP4 = true;

                       break;

                   }

                   dwSubLeft --;

                   sSubPtr ++;

              }

              if (bMP4) {

                   iRet = 5;

                   break;

              }

              dwLeft -= 188;

              sPtr += 188;;

         }

         if (iRet == -1)        // Can not find MPEG-4 Stuff, so MPEG-2

              iRet = 3;

     } else                      // We ignore ES checking in this code

         iRet = -1;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在使用Python提取GPS信息时,我们可以使用多种方法。 第一种方法是使用模块GPSd和GPS3。这两个模块提供了与GPS接收器通信的功能。你可以连接到GPS接收器,并从中获取经纬度、海拔等GPS信息。这种方法的优点是它提供了一个简单的接口,可以快速地获取GPS信息。但缺点是它需要一个GPS接收器,而且有可能会受到接收器的限制。 第二种方法是使用模块PySerial。该模块可以帮助你与GPS设备建立串行通信。通过读取设备发送的数据,你可以提取其中的GPS信息。这种方法的好处是它可以与任何串行GPS设备兼容,并且不需要额外的软件支持。但缺点是你需要自己解析接收到的数据,并提取GPS信息。 第三种方法是使用第三方库,如geopy。该库提供了多种方法来获取和处理GPS信息。你可以使用它来解析GPS坐标、计算两个坐标之间的距离等。这种方法的优点是它提供了许多便利的功能,并且不需要额外的设备或软件支持。但缺点是它可能需要额外的安装和配置,并且有时可能不够灵活。 无论你选择哪种方法,记住要确保你的代码能够正确地连接到GPS设备,并能够正确解析接收到的数据。此外,还要注意处理异常情况,如设备无法连接或接收到无效的数据。 ### 回答2: 在Python中,我们可以使用各种库和模块来提取GPS信息。其中,最常用的库是`gpxpy`和`geopy`。 首先,我们可以使用`gpxpy`库来解析GPX文件,该文件通常包含GPS轨迹数据。我们可以使用`open`函数打开GPX文件,然后使用`GPXTrack`和`GPXTrackSegment`对象来提取轨迹数据。我们可以遍历每个段的点,获取经度(longitude)和纬度(latitude)等信息。 另外,我们还可以使用`geopy`库来提取和处理GPS坐标信息。该库提供了一系列功能,如逆地理编码(从经纬度获取地址)、距离计算、坐标转换等等。我们可以使用`Nominatim`类来进行逆地理编码。给定经度和纬度,我们可以调用该类的`reverse`方法,获取具体的地址。 示例代码如下: ```python # 使用gpxpy库提取GPS信息 import gpxpy def extract_gps_info(filename): gpx_file = open(filename, 'r') gpx = gpxpy.parse(gpx_file) for track in gpx.tracks: for segment in track.segments: for point in segment.points: print('经度:', point.longitude) print('纬度:', point.latitude) # 使用geopy库进行逆地理编码 from geopy.geocoders import Nominatim def reverse_geocoding(lat, lon): geolocator = Nominatim(user_agent="my_app") location = geolocator.reverse(f"{lat}, {lon}") print('地址:', location.address) # 调用函数进行提取和处理 extract_gps_info('track.gpx') reverse_geocoding(51.5074, -0.1278) ``` 通过以上代码,我们可以提取GPX文件中的GPS信息,并且将给定的经纬度转换为具体的地址信息。请注意,你需要在代码中替换实际的GPX文件名和经纬度值。 ### 回答3: Python可以通过使用适当的库和模块来提取GPS信息。其中最常用的库是`gpsd`。 首先,需要在系统中安装`gpsd`库。可以使用以下命令安装: `pip install gpsd-py3` 然后,可以使用以下代码来提取GPS信息: ```python import gpsd # 连接到GPS gpsd.connect() # 获取GPS信息 packet = gpsd.get_current() # 提取所需信息 latitude = packet.position()[0] # 获取纬度 longitude = packet.position()[1] # 获取经度 altitude = packet.altitude() # 获取海拔 speed = packet.speed() # 获取速度 time = packet.time() # 获取时间 # 打印提取信息 print("纬度:", latitude) print("经度:", longitude) print("海拔:", altitude) print("速度:", speed) print("时间:", time) ``` 此代码将连接到GPS设备并获取当前的GPS信息。然后,从返回的数据包中提取所需的信息,如纬度、经度、海拔、速度和时间。最后,将提取信息打印出来。 此外,还可以使用其他库,如`pyserial`和`pynmea2`,来解析NMEA格式的GPS数据。这些库可以用于连接到GPS设备并从串口接收和解析GPS数据。 总之,通过使用适当的Python库和模块,可以很容易地提取和处理GPS信息

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值