Skip to content

Commit

Permalink
working. fixed width naming convention. multiple videos works too. v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
masaaldosey committed Jan 6, 2021
1 parent db9367e commit a7b7921
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 204 deletions.
18 changes: 16 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Video-to-Frames

Simple python script to extract and store individual frames of a given video.
Simple python script to extract and store individual frames of a given video. If you have multiple videos for frame extraction, store them under a common `source directory` and then run the script as is.

The extracted frames will be stored under a separate `output directory`. In case of multiple videos, the script creates a separate folder for each video in the `output directory`.

## Setup
1. Clone the repository using `git`.
Expand All @@ -10,4 +12,16 @@ Simple python script to extract and store individual frames of a given video.
sudo apt-get update && install ffmpeg
```

## TO DO: update usage
## Usage
Change the following under `main` in `split_video.py`:
1. `output_path`: path to store extracted frames of each video,
2. `input_path`: path containing the videos to extract frames from,
2. `pattern`: extension of the video files - `.mp4`, `.mkv` etc.

---

<br>

## TO DO
- In case of multiple video extraction, add a boilerplate which enables creation of a dataframe containing details of extracted frames.

155 changes: 0 additions & 155 deletions create_dataframe.py

This file was deleted.

38 changes: 0 additions & 38 deletions rename_frames_fixwidth.py

This file was deleted.

36 changes: 27 additions & 9 deletions split_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@

def videos_to_imgs(output_path=None,
input_path=None,
pattern="*.mp4"):
pattern='*.mp4'):
"""Function to split an input video into frames. It saves the frames with
a naming convention `videoname_f000000_t0000.00.png` where - second term represents
the frame number and the third term represents its corresponding time-stamp. This function scales down
the video to a `250x250` resolution and also saves a `.txt` file containing time-stamps of the extracted
frames for additional use by the user. User can put multiple videos in a directory under `input_path` and
the extracted frames for each video will be stored in its own separate directory under `output_path`.
Args:
output_path (Path): Absolute path to the directory to store the extracted frames. Defaults to None.
input_path (Path): Absolute path to the directory which contains the videos to extract frames from. Defaults to None.
pattern (str, optional): Extension of the video files to extract frames from. Defaults to `*.mp4`.
"""

output_path = Path(output_path)
input_path = Path(input_path)
Expand All @@ -25,6 +37,7 @@ def videos_to_imgs(output_path=None,
out_folder = output_path / file_name
out_folder.mkdir(exist_ok=True)

print(f'Start Writing time-stamps for Video {i+1}')
# Writing time-stamps of each frame to `.txt` file
os.system(
f'ffprobe {video_path} -select_streams v -show_entries frame=coded_picture_number,pkt_pts_time -of csv=p=0:nk=1 -v 0 > {out_folder/file_name}.txt'
Expand All @@ -38,28 +51,33 @@ def videos_to_imgs(output_path=None,
times_dict = {}
with open(f'{out_folder/file_name}.txt') as file:
for line in file:
(key, val) = line.strip().split(",")
(key, val) = line.strip().split(',')
times_dict[val] = f'{float(key):.2f}'
print(f'End Writing time-stamps for Video {i+1}')

# Adding time-stamp corresponding to each frame in its name
frames = list(out_folder.glob("*.png"))
frames = list(out_folder.glob('*.png'))
frames.sort()
os.chdir(out_folder)
# Renaming loop
# Rename loop.
for frame in frames:
try:
new_name = file_name + '_f' + frame.name.split('_')[1].split('.')[0] + '_t' + f'{times_dict[frame.stem.split("_")[1]]}' + '.png'
# vidname_f000000_t0000.00.png
new_name = (file_name + '_f' + format(int(frame.name.split('_')[1].split('.')[0]), '06d') +
'_t' + format(float(times_dict[frame.stem.split("_")[1]]), '07.2f') + '.png')

frame.rename(new_name)

except:
frame.unlink()

# Print to terminal after completion of extracting each video
print("Done extracting: {}".format(i + 1))
print(f'Done extracting: {i+1}')
print(f'Number of frames extracted from Video {i+1}: {len(frames)}')


if __name__ == "__main__":
videos_to_imgs(output_path="/home/prabhat/Videos/output",
input_path="/home/prabhat/Videos/input",
pattern="*.mp4")
# Change `output_path` and `input_path` accordingly
videos_to_imgs(output_path='/home/prabhat/Videos/output',
input_path='/home/prabhat/Videos/input',
pattern='*.mp4')

0 comments on commit a7b7921

Please sign in to comment.