I am trying to cut the video into sections by the frames , but the code is not working
In the code I have a list of tuples (start frame,end frame) and I want to take the whole list and create a new video file from it, the problem it takes only the last couple on the list
import cv2
vidPath = "wingate_18-11-18_3mp_25m_pm_4m(3).mp4"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size =
(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
for idx, (begFidx, endFidx) in enumerate(segRange):
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
The new video was created from 200 to 400 , not including any frames before.
thanks very much for the help
python opencv video-processing
add a comment |
In the code I have a list of tuples (start frame,end frame) and I want to take the whole list and create a new video file from it, the problem it takes only the last couple on the list
import cv2
vidPath = "wingate_18-11-18_3mp_25m_pm_4m(3).mp4"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size =
(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
for idx, (begFidx, endFidx) in enumerate(segRange):
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
The new video was created from 200 to 400 , not including any frames before.
thanks very much for the help
python opencv video-processing
add a comment |
In the code I have a list of tuples (start frame,end frame) and I want to take the whole list and create a new video file from it, the problem it takes only the last couple on the list
import cv2
vidPath = "wingate_18-11-18_3mp_25m_pm_4m(3).mp4"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size =
(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
for idx, (begFidx, endFidx) in enumerate(segRange):
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
The new video was created from 200 to 400 , not including any frames before.
thanks very much for the help
python opencv video-processing
In the code I have a list of tuples (start frame,end frame) and I want to take the whole list and create a new video file from it, the problem it takes only the last couple on the list
import cv2
vidPath = "wingate_18-11-18_3mp_25m_pm_4m(3).mp4"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size =
(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
for idx, (begFidx, endFidx) in enumerate(segRange):
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
The new video was created from 200 to 400 , not including any frames before.
thanks very much for the help
python opencv video-processing
python opencv video-processing
asked Dec 27 at 12:56
yoel
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It's because your writer overwrite the 1-40 and 50-100 by the last one 200-400 in the file name shotsPath=new.avi. Declare a new writer to new file with each segment.
[EDIT]
import cv2
vidPath = "1.MOV"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
#declare writer before the for loop
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
for idx, (begFidx, endFidx) in enumerate(segRange):
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
print(frame_number)
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
#Check how many frame that new.avi has
cap2 = cv2.VideoCapture("new.avi")
print(cap2.get(cv2.CAP_PROP_FRAME_COUNT))
>> 290 frames
The result is correct.
Thank you , the thing is that if for everyone i need to make a new file then how do I connect all the file for one video ?
– yoel
Dec 27 at 13:29
Oh, so you want all those segments in one file? I just edit my answer.
– Ha Bom
Dec 27 at 14:20
Sorry but I probably did not understand something because now he only takes the first piece (0,40), thanks
– yoel
Dec 27 at 14:43
@yoel I've printed out the indexes of the frames and the total number of the frames, and it's correct. Try out the full code that I just edited in my answer.
– Ha Bom
Dec 27 at 15:00
thanks very much
– yoel
Dec 27 at 15:20
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945473%2fi-am-trying-to-cut-the-video-into-sections-by-the-frames-but-the-code-is-not-w%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's because your writer overwrite the 1-40 and 50-100 by the last one 200-400 in the file name shotsPath=new.avi. Declare a new writer to new file with each segment.
[EDIT]
import cv2
vidPath = "1.MOV"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
#declare writer before the for loop
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
for idx, (begFidx, endFidx) in enumerate(segRange):
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
print(frame_number)
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
#Check how many frame that new.avi has
cap2 = cv2.VideoCapture("new.avi")
print(cap2.get(cv2.CAP_PROP_FRAME_COUNT))
>> 290 frames
The result is correct.
Thank you , the thing is that if for everyone i need to make a new file then how do I connect all the file for one video ?
– yoel
Dec 27 at 13:29
Oh, so you want all those segments in one file? I just edit my answer.
– Ha Bom
Dec 27 at 14:20
Sorry but I probably did not understand something because now he only takes the first piece (0,40), thanks
– yoel
Dec 27 at 14:43
@yoel I've printed out the indexes of the frames and the total number of the frames, and it's correct. Try out the full code that I just edited in my answer.
– Ha Bom
Dec 27 at 15:00
thanks very much
– yoel
Dec 27 at 15:20
|
show 1 more comment
It's because your writer overwrite the 1-40 and 50-100 by the last one 200-400 in the file name shotsPath=new.avi. Declare a new writer to new file with each segment.
[EDIT]
import cv2
vidPath = "1.MOV"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
#declare writer before the for loop
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
for idx, (begFidx, endFidx) in enumerate(segRange):
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
print(frame_number)
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
#Check how many frame that new.avi has
cap2 = cv2.VideoCapture("new.avi")
print(cap2.get(cv2.CAP_PROP_FRAME_COUNT))
>> 290 frames
The result is correct.
Thank you , the thing is that if for everyone i need to make a new file then how do I connect all the file for one video ?
– yoel
Dec 27 at 13:29
Oh, so you want all those segments in one file? I just edit my answer.
– Ha Bom
Dec 27 at 14:20
Sorry but I probably did not understand something because now he only takes the first piece (0,40), thanks
– yoel
Dec 27 at 14:43
@yoel I've printed out the indexes of the frames and the total number of the frames, and it's correct. Try out the full code that I just edited in my answer.
– Ha Bom
Dec 27 at 15:00
thanks very much
– yoel
Dec 27 at 15:20
|
show 1 more comment
It's because your writer overwrite the 1-40 and 50-100 by the last one 200-400 in the file name shotsPath=new.avi. Declare a new writer to new file with each segment.
[EDIT]
import cv2
vidPath = "1.MOV"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
#declare writer before the for loop
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
for idx, (begFidx, endFidx) in enumerate(segRange):
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
print(frame_number)
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
#Check how many frame that new.avi has
cap2 = cv2.VideoCapture("new.avi")
print(cap2.get(cv2.CAP_PROP_FRAME_COUNT))
>> 290 frames
The result is correct.
It's because your writer overwrite the 1-40 and 50-100 by the last one 200-400 in the file name shotsPath=new.avi. Declare a new writer to new file with each segment.
[EDIT]
import cv2
vidPath = "1.MOV"
shotsPath = "new.avi"
segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
cap = cv2.VideoCapture(vidPath)
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = int(cv2.VideoWriter_fourcc('X','V','I','D')) # XVID codecs
#declare writer before the for loop
writer = cv2.VideoWriter(shotsPath, fourcc, fps, size)
for idx, (begFidx, endFidx) in enumerate(segRange):
cap.set(cv2.CAP_PROP_POS_FRAMES, begFidx)
ret = True # has frame returned
while (cap.isOpened() and ret and writer.isOpened()):
ret, frame = cap.read()
frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
print(frame_number)
if frame_number < endFidx:
writer.write(frame)
else:
break
writer.release()
#Check how many frame that new.avi has
cap2 = cv2.VideoCapture("new.avi")
print(cap2.get(cv2.CAP_PROP_FRAME_COUNT))
>> 290 frames
The result is correct.
edited Dec 27 at 14:59
answered Dec 27 at 13:04
Ha Bom
6091418
6091418
Thank you , the thing is that if for everyone i need to make a new file then how do I connect all the file for one video ?
– yoel
Dec 27 at 13:29
Oh, so you want all those segments in one file? I just edit my answer.
– Ha Bom
Dec 27 at 14:20
Sorry but I probably did not understand something because now he only takes the first piece (0,40), thanks
– yoel
Dec 27 at 14:43
@yoel I've printed out the indexes of the frames and the total number of the frames, and it's correct. Try out the full code that I just edited in my answer.
– Ha Bom
Dec 27 at 15:00
thanks very much
– yoel
Dec 27 at 15:20
|
show 1 more comment
Thank you , the thing is that if for everyone i need to make a new file then how do I connect all the file for one video ?
– yoel
Dec 27 at 13:29
Oh, so you want all those segments in one file? I just edit my answer.
– Ha Bom
Dec 27 at 14:20
Sorry but I probably did not understand something because now he only takes the first piece (0,40), thanks
– yoel
Dec 27 at 14:43
@yoel I've printed out the indexes of the frames and the total number of the frames, and it's correct. Try out the full code that I just edited in my answer.
– Ha Bom
Dec 27 at 15:00
thanks very much
– yoel
Dec 27 at 15:20
Thank you , the thing is that if for everyone i need to make a new file then how do I connect all the file for one video ?
– yoel
Dec 27 at 13:29
Thank you , the thing is that if for everyone i need to make a new file then how do I connect all the file for one video ?
– yoel
Dec 27 at 13:29
Oh, so you want all those segments in one file? I just edit my answer.
– Ha Bom
Dec 27 at 14:20
Oh, so you want all those segments in one file? I just edit my answer.
– Ha Bom
Dec 27 at 14:20
Sorry but I probably did not understand something because now he only takes the first piece (0,40), thanks
– yoel
Dec 27 at 14:43
Sorry but I probably did not understand something because now he only takes the first piece (0,40), thanks
– yoel
Dec 27 at 14:43
@yoel I've printed out the indexes of the frames and the total number of the frames, and it's correct. Try out the full code that I just edited in my answer.
– Ha Bom
Dec 27 at 15:00
@yoel I've printed out the indexes of the frames and the total number of the frames, and it's correct. Try out the full code that I just edited in my answer.
– Ha Bom
Dec 27 at 15:00
thanks very much
– yoel
Dec 27 at 15:20
thanks very much
– yoel
Dec 27 at 15:20
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945473%2fi-am-trying-to-cut-the-video-into-sections-by-the-frames-but-the-code-is-not-w%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown