Skip to content

Open-Sora: Democratizing Efficient Video Production for All

Python

[feat] support ffmpeg

Zangwei Zheng8fa108a606b4
diff --git a/tools/datasets/README.md b/tools/datasets/README.md
index d5d6bbb..b3668b1 100644
--- a/tools/datasets/README.md
+++ b/tools/datasets/README.md
@@ -84,12 +84,18 @@ To accelerate processing speed, you can install [pandarallel](https://github.com
pip install pandarallel
```
-To get video information, you need to install [opencv-python](https://github.com/opencv/opencv-python):
+To get image and video information, you need to install [opencv-python](https://github.com/opencv/opencv-python):
```bash
pip install opencv-python
```
+Or to get video information, you can install ffmpeg and ffmpeg-python:
+
+```bash
+pip install ffmpeg-python
+```
+
To filter a specific language, you need to install [lingua](https://github.com/pemistahl/lingua-py):
```bash
@@ -167,7 +173,8 @@ You can also use `python -m tools.datasets.csvutil --help` to see usage.
| `--sort-descending KEY` | `_sort` | Sort the dataset by KEY in descending order |
| `--difference DATA.csv` | | Remove the paths in DATA.csv from the dataset |
| `--intersection DATA.csv` | | Keep the paths in DATA.csv from the dataset and merge columns |
-| `--info` | `_info` | Get the basic information of each video and image |
+| `--info` | `_info` | Get the basic information of each video and image (cv2) |
+| `--video-info` | `_vinfo` | Get the basic information of each video (ffmpeg) |
| `--ext` | `_ext` | Remove rows if the file does not exist |
| `--remove-corrupted` | `_nocorrupted` | Remove the corrupted video and image |
| `--relpath` | `_relpath` | Modify the path to relative path by root given |
diff --git a/tools/datasets/csvutil.py b/tools/datasets/csvutil.py
index addf2a3..45fdbb6 100644
--- a/tools/datasets/csvutil.py
+++ b/tools/datasets/csvutil.py
@@ -46,7 +46,7 @@ def apply(df, func, **kwargs):
# ======================================================
-def get_info_cv2(path):
+def get_info(path):
import cv2
try:
@@ -72,11 +72,25 @@ def get_info_cv2(path):
return 0, 0, 0, np.nan, np.nan, np.nan
-def get_info_ffmpeg(path):
+# ======================================================
+# --video-info
+# ======================================================
+
+
+def get_video_info(path):
import ffmpeg
- ffmpeg.probe(path)
- breakpoint()
+ try:
+ info = ffmpeg.probe(path)["streams"][0]
+ height = int(info["height"])
+ width = int(info["width"])
+ num_frames = int(info["nb_frames"])
+ aspect_ratio = height / width
+ hw = height * width
+ fps = np.nan
+ return num_frames, height, width, aspect_ratio, fps, hw
+ except:
+ return 0, 0, 0, np.nan, np.nan, np.nan
# ======================================================
@@ -474,7 +488,18 @@ def main(args):
assert "text" in data.columns
data["text_len"] = apply(data["text"], lambda x: len(tokenizer(x)["input_ids"]))
if args.info:
- info = apply(data["path"], get_info_ffmpeg)
+ info = apply(data["path"], get_info)
+ (
+ data["num_frames"],
+ data["height"],
+ data["width"],
+ data["aspect_ratio"],
+ data["fps"],
+ data["resolution"],
+ ) = zip(*info)
+ if args.video_info:
+ assert "path" in data.columns
+ info = apply(data["path"], get_video_info)
(
data["num_frames"],
data["height"],
@@ -552,6 +577,7 @@ def parse_args():
# IO-related
parser.add_argument("--info", action="store_true", help="get the basic information of each video and image")
+ parser.add_argument("--video-info", action="store_true", help="get the basic information of each video")
parser.add_argument("--ext", action="store_true", help="check if the file exists")
parser.add_argument("--remove-corrupted", action="store_true", help="remove the corrupted video and image")
@@ -612,6 +638,8 @@ def get_output_path(args, input_name):
# IO-related
if args.info:
name += "_info"
+ if args.video_info:
+ name += "_vinfo"
if args.ext:
name += "_ext"
if args.remove_corrupted: