Skip to content

Commit

Permalink
Add documents about querying metadata of media using ffprobe
Browse files Browse the repository at this point in the history
  • Loading branch information
jonghwanhyeon committed Nov 30, 2023
1 parent 065b289 commit 4721474
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
70 changes: 70 additions & 0 deletions docs/examples/querying-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Querying metadata

You can use **python-ffmpeg** to query the metadata of an input file using `ffprobe`.

=== "Synchronous API"

```python
import json

from ffmpeg import FFmpeg


def main():
ffprobe = FFmpeg(executable="ffprobe").input(
"input.mp4",
print_format="json", # ffprobe will output the results in JSON format
show_streams=None,
)

media = json.loads(ffprobe.execute())

print(f"# Video")
print(f"- Codec: {media['streams'][0]['codec_name']}")
print(f"- Resolution: {media['streams'][0]['width']} X {media['streams'][0]['height']}")
print(f"- Duration: {media['streams'][0]['duration']}")
print("")

print(f"# Audio")
print(f"- Codec: {media['streams'][1]['codec_name']}")
print(f"- Sample Rate: {media['streams'][1]['sample_rate']}")
print(f"- Duration: {media['streams'][1]['duration']}")


if __name__ == "__main__":
main()
```

=== "Asynchronous API"

``` python
import asyncio
import json

from ffmpeg.asyncio import FFmpeg


async def main():
ffprobe = FFmpeg(executable="ffprobe").input(
"input.mp4",
print_format="json", # ffprobe will output the results in JSON format
show_streams=None,
)

media = json.loads(await ffprobe.execute())

print(f"# Video")
print(f"- Codec: {media['streams'][0]['codec_name']}")
print(f"- Resolution: {media['streams'][0]['width']} X {media['streams'][0]['height']}")
print(f"- Duration: {media['streams'][0]['duration']}")
print("")

print(f"# Audio")
print(f"- Codec: {media['streams'][1]['codec_name']}")
print(f"- Sample Rate: {media['streams'][1]['sample_rate']}")
print(f"- Duration: {media['streams'][1]['duration']}")


if __name__ == "__main__":
asyncio.run(main())
```
29 changes: 29 additions & 0 deletions examples/asyncio/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import asyncio
import json

from ffmpeg.asyncio import FFmpeg


async def main():
ffmpeg = FFmpeg(executable="ffprobe").input(
"input.mp4",
print_format="json", # ffprobe will output the results in JSON format
show_streams=None,
)

media = json.loads(await ffmpeg.execute())

print(f"# Video")
print(f"- Codec: {media['streams'][0]['codec_name']}")
print(f"- Resolution: {media['streams'][0]['width']} X {media['streams'][0]['height']}")
print(f"- Duration: {media['streams'][0]['duration']}")
print("")

print(f"# Audio")
print(f"- Codec: {media['streams'][1]['codec_name']}")
print(f"- Sample Rate: {media['streams'][1]['sample_rate']}")
print(f"- Duration: {media['streams'][1]['duration']}")


if __name__ == "__main__":
asyncio.run(main())
30 changes: 30 additions & 0 deletions examples/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import annotations

import json

from ffmpeg import FFmpeg


def main():
ffmpeg = FFmpeg(executable="ffprobe").input(
"input.mp4",
print_format="json",
show_streams=None,
)

media = json.loads(ffmpeg.execute())

print(f"# Video")
print(f"- Codec: {media['streams'][0]['codec_name']}")
print(f"- Resolution: {media['streams'][0]['width']} X {media['streams'][0]['height']}")
print(f"- Duration: {media['streams'][0]['duration']}")
print("")

print(f"# Audio")
print(f"- Codec: {media['streams'][1]['codec_name']}")
print(f"- Sample Rate: {media['streams'][1]['sample_rate']}")
print(f"- Duration: {media['streams'][1]['duration']}")


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ nav:
- examples/feeding-data-to-stdin.md
- examples/monitoring-status.md
- examples/asynchronous-listeners.md
- examples/querying-metadata.md
- examples/handling-errors.md
- api.md

Expand Down

0 comments on commit 4721474

Please sign in to comment.