# Date, time, and duration from a video file

**URL:** <https://discourse.julialang.org/t/date-time-and-duration-from-a-video-file/5799>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 9, 2017, 3:15pm UTC](https://discourse.julialang.org/t/date-time-and-duration-from-a-video-file/5799 "2017-09-09T15:15:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 9, 2017, 3:15pm UTC](https://discourse.julialang.org/t/date-time-and-duration-from-a-video-file/5799/1 "2017-09-09T15:15:12Z")

</div>

I need to get the `original date and time` as well as `duration` from video files. It looks like the only option to accomplish this right now is via external tools (see [here](https://discourse.julialang.org/t/is-there-any-way-i-can-get-the-playing-time-of-a-movie-file/2894/2)).

But can anyone here point me to the right direction (if at all possible) for how to accomplish this with [`VideoIO.jl`](https://github.com/kmsquire/VideoIO.jl)?

Thanks!

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [September 9, 2017, 9:47pm UTC](https://discourse.julialang.org/t/date-time-and-duration-from-a-video-file/5799/2 "2017-09-09T21:47:11Z")

</div>

Hello Yakir,

Sorry I didn’t answer you in your issue at VideoIO.jl.

> I need to get the original date and time as well as duration from video files

It is actually possible, although not that pretty. But here goes…

When you open a file, you get back an `AVInput` object. This contains a pointer to an `AVFormatContext` (stored in an array to mimic the original C struct). The `AVFormatContext` struct contains a `duration` field (see [here](https://www.ffmpeg.org/doxygen/3.2/structAVFormatContext.html#ad0ea78ac48f5bb0a15a15c1c472744d9)) and a `start_time_realtime` field (see [here](https://www.ffmpeg.org/doxygen/3.2/structAVFormatContext.html#aa5ddb5cee1df28f21739133f2e37f1c5)).

Duration is stored in terms of `1 / AV_TIME_BASE`, so you have to divide by `AV_TIME_BASE` to get the time in seconds.

`start_time_realtime` is stored in “microseconds since the Unix epoch (00:00 1st January 1970)” (from the documentation) which is pretty standard. If this isn’t stored in the video, `start_time_realtime` has the value `AV_NOPTS_VALUE`.

In code, this would look something like this:

```julia
v = VideoIO.testvideo("annie_oakley")
fc = unsafe_load(v.apFormatContext[1])
duration = fc.duration / VideoIO.AV_TIME_BASE
start_time_realtime = fc.start_time_realtime
no_start_time = (start_time_realtime == VideoIO.AV_NOPTS_VALUE)

println("Duration: $duration seconds")
if no_start_time
    println("Start time not available")
else
    println("start_time_realtime: $start_time_realtime")
end

```

Output:

```julia
Duration: 24.2242 seconds
Start time not available

```

If you wanted to provide a better interface for this in VideoIO.jl, a PR would be very welcome!

(Also note that VideoIO.jl is broken for actually reading videos for ffmpeg versions after 3.1, although I finally [figured out a fix yesterday](https://github.com/kmsquire/VideoIO.jl/pull/108).)

Best,  
Kevin
