Last active
May 9, 2023 11:15
-
-
Save FreeFly19/fea8f0a073800899166fa5cd666ffbe3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import av | |
import tqdm | |
from av.video import VideoStream | |
desired_fps = 2 | |
timestamp = int(time.time()) | |
input_container = av.open('m3u8_To_MP4.mp4') | |
output_container = av.open(f"output_{timestamp}.mp4", "w") | |
in_video_stream: VideoStream = input_container.streams.video[0] | |
out_video_stream = output_container.add_stream('h264', rate=desired_fps) | |
out_video_stream.width = in_video_stream.width | |
out_video_stream.height = in_video_stream.height | |
out_video_stream.pix_fmt = in_video_stream.pix_fmt | |
out_video_stream.bit_rate = 16000000 | |
original_fps = int(in_video_stream.average_rate) | |
save_each_n_frames = round(original_fps/desired_fps) | |
print(save_each_n_frames) | |
try: | |
for i, frame in tqdm.tqdm(enumerate(input_container.decode(video=0))): | |
if i % save_each_n_frames == 0: | |
frame.pts = None | |
frame.time_base = None | |
packet = out_video_stream.encode(frame) | |
output_container.mux(packet) | |
finally: | |
input_container.close() | |
output_container.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment