Generate new video from selected seconds of a longer video - python-3.x

I try to generate a shorter version of a 10min original videofile.mp4 that includes only four 10s subclips of the original file (i.e. from seconds 10 to 20;197 to 207;393 to 403;570 to 580). So far I could only generate 4 new files copying both video and audio:
ffmpeg -i videofile.mp4 -vcodec copy -acodec copy -ss 10 -to 20 videofile1.mp4 -vcodec copy -acodec copy -ss 197 -to 207 videofile2.mp4 -vcodec copy -acodec copy -ss 393 -to 403 videofile3.mp4 -vcodec copy -acodec copy -ss 570 -to 580 videofile4.mp4
However, I am having deep trouble in concatenating those 4 subclips to generate the desired 40seconds out_videofile.mp4. I found this alternative approach using the "select" command in ffmpeg which saves me of my "failed" concatenation process. So far I have:
ffmpeg -i videofile.mp4 -vf "select='between(t,10,20)+between(t,197,207)+between(t,393,403)+between(t,570,580)',setpts=N/FRAME_RATE/TB" -af "aselect='between(t,10,20)+between(t,197,207)+between(t,393,403)+between(t,570,580)" out_videofile.mp4
I guess this last code should give me the 40s out_videofile.mp4 I want. However, it gives me a "SyntaxError: invalid syntax". Any idea where I go wrong?
Thanks for your time anyways.

You're missing a closing quote, but also a filter for smoothing the audio timestamps.
Use
ffmpeg -i videofile.mp4 -vf "select='between(t,10,20)+between(t,197,207)+between(t,393,403)+between(t,570,580)',setpts=N/FRAME_RATE/TB" -af "aselect='between(t,10,20)+between(t,197,207)+between(t,393,403)+between(t,570,580)',asetpts=N/SR/TB" out_videofile.mp4

Related

ffmpeg split | merge video voice not sync

there are what i done:
download a full mp4 file.
due to it's watermark(0s-10s), i split the full video into 2 parts from 10second. the first part with watermark.
use ffmpeg delogo the first part.
merge the two video into a full again.
wget -O download.mp4
ffmpeg -i download.mp4 -vcodec copy -acodec copy -t 00:00:10 tmp1.mp4
ffmpeg -i download.mp4 -vcodec copy -acodec copy -ss 00:00:10 tmp2.mp4
ffmpeg -i tmp1.mp4 -vf "delogo=x=432:y=44:w=1060:h=108" -c:a copy tmp3.mp4
echo file tmp3.mp4 > mergelist.txt && echo file tmp2.mp4 >> mergelist.txt
ffmpeg -f concat -i mergelist.txt -c copy output.mp4
problem i faced:
in the last merged video, only one tmp part is fine, the other's video and voice not sync and play time more faster than before.
why i divide it, delogo(although only the first 10 seconds shows) full video more than 1h, re-encode takes much time, 10s part fine to me.
Have you tried this?
ffmpeg -i download.mp4 -vf "delogo=enable='lte(t,10)':x=432:y=44:w=1060:h=108" -c:a copy output.mp4
I'm assuming delogo filter is timeline editing enabled.

How to make ffmpeg respect the "start_time" variable when combining cdg and mp3?

I'm trying to combine two .cdg and .mp3 files because I want to make a single .mkv file out of them.
When I do it I get a .mkv file alright, but the audio is unfortunately not synced with the video.
So, I was wondering why this is? Apparently I must be missing a parameter on my command or something.
What I do is this:
ffmpeg -y -i song.cdg -i song.mp3 -pix_fmt yuv420p -vcodec libx264 -acodec copy song.mkv
It works fine, but in this particular case the result is out of sync by ~1.2 seconds. This varies per song, from 0 to a couple of seconds.
However, if I do a ffprobe on the .cdg file I notice that this number is already present, as the "start_time" variable:
ffprobe -v error -show_format -show_streams song.cdg
...
start_time=1.186667
...
So, if I just use this number directly as this for the -itsoffset parameter:
ffmpeg -y -itsoffset 1.186667 -i song.cdg -i song.mp3 -pix_fmt yuv420p -vcodec libx264 -acodec copy song.mkv
Then the video and audio is in perfect sync.
So, I am wondering - what parameter do I need to add to my command to have ffmpeg respect this variable?
I tried to add the -copyts parameter and it solved my problem.
ffmpeg -copyts -y -i song.cdg -i song.mp3 -pix_fmt yuv420p -vcodec libx264 -acodec copy song.mkv
https://ffmpeg.org/ffmpeg.html
-copyts
Do not process input timestamps, but keep their values without trying to sanitize them. In particular, do not remove the initial
start time offset value.
Note that, depending on the vsync option or on specific muxer
processing (e.g. in case the format option avoid_negative_ts is
enabled) the output timestamps may mismatch with the input timestamps
even when this option is selected.

ffmpeg correct audio streams when appending

I am appending 2 mp4 files together using the following routine:
ffmpeg -y -i one.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy one.ts
ffmpeg -y -i two.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy two.ts
cat one.ts two.ts >> joined.ts
ffmpeg -y -i joined.ts -vcodec copy -acodec copy -absf aac_adtstoasc joined.mp4
This works fine, however when one.mp4 has no audio stream (the file is created using ffmpeg from a single jpeg, so no sound), the routine appends the files but the audio of two.mp4 starts at the beginning of the video. how should I solve this problem? Should I add switches to my append routine so that the audio in two.mp4 starts playing where it should, or should I add something to the ffmpeg command which creates the video from jpg?
here is the command I use to create one.mp4 which contains no sound:
ffmpeg -y -loop 1 -i blah.jpg -t 3 -vcodec libx264 one.mp4

ffmpeg stream offset command (-itsoffset) not working

I would really appreciate if someone could give some pointers regarding the use of itsoffset with ffmpeg. I have read a number of posts on this subject, some of them explain very clearly how to re-synchronize audio and video with -itsoffset, but I haven't been able to make it work.
My avi file is encoded with ffmpeg, in two passes, using the following command for the second pass:
ffmpeg -i whole-vts_01.avs -pass 2 -y -vcodec libxvid -vtag XVID -b:v 1300K -g 240 -trellis 2 -mbd rd -flags +mv4+aic -acodec ac3 -ac 2 -ar 48000 -b:a 128k output.avi
For whatever reason, I end up with a 1 sec delay in the video (or the audio is 1 sec early). It doesn't happen too often but I see it from time to time.
Among other attempts, I have tried the following:
(1) ffmpeg -i output.avi -itsoffset 00:00:01.0 -i output.avi -vcodec copy -acodec copy -map 0:0 -map 1:1 output-resynched.avi
(2) ffmpeg -i output.avi -itsoffset 00:00:01.0 -i output.ac3 -vcodec copy -acodec copy -map 0:0 -map 1:0 output-resynched2.avi
(3) ffmpeg -itsoffset -00:00:01.00 -i output.avi output-resynched8.avi
(4) ffmpeg -i output.avi -itsoffset -1.0 -i output.avi -vcodec copy -acodec copy -map 0:1 -map 1:0 output-resynched13.avi
Here are the results:
Audio garbled and only 5m 35 s long vs. 1h 41m.
(Output.ac3 is audio component of output.avi) Video and audio
identical to original, offset didn't work
Audio did get shifted, but original encoding parameters replaced with default ones (as expected).
Audio garbled and only 9m 56s long vs. 1h 41m.
I see that many people explain, and apparently use the process described above, but it doesn't seem to be working for me. Am I missing something obvious? I would very much like to be able to use -itsoffset as it is cleaner than my workaround solution.
FWIW, here is a different, and longer way of obtaining the desired result:
First create a shifted video only file using -ss:
ffmpeg -i output.avi -ss 1.0 -vcodec copy -an oupput_videoshifted.avi
Then extract the audio:
ffmpeg -i output.avi -vn -acodec copy outputaudioonly.ac3
And finally remux both components:
ffmpeg -i output_videoshifted.avi -i output_audioonly.ac3 -vcodec copy -acodec copy -map 0:0 -map 1:0 output-resynched14.avi
The process works, is fast enough, but I would really prefer to use the one pass -itsoffset solution.
Here is what I did and it work for me
The first input setting -i and the second input is come from the same one video file.
Delay 1 second in first input video and the second input audio just make a copy
ffmpeg -y -itsoffset 00:00:01.000 -i "d:\Video1.mp4" -i "d:\Video1.mp4"
-map 0:v -map 1:a -vcodec copy -acodec copy
-f mp4 -threads 2 -v warning "Video2.mp4"
Delay 1 second in second input audio and the first input video just make a copy
ffmpeg -y -i "d:\Video1.mp4" -itsoffset 00:00:01.000 -i "d:\Video1.mp4"
-map 0:v -map 1:a -vcodec copy -acodec copy
-f mp4 -threads 2 -v warning "Video2.mp4"
The problem is located on -vcodec copy -acodec copy because the shifting will only work on keyframes. I have had the same problem.
Just don't copy (audio/)video, try the thing with -itsoffset, but use
-vcodec libxvid -vtag XVID -b:v 1300K -g 240 -trellis 2 -mbd rd -flags +mv4+aic -acodec ac3 -ac 2 -ar 48000 -b:a 128k
for re-encoding. It should work.

How to script this, so output is used as input?

I would like to script this command
ffmpeg -i concat:file1.mp3\|file2.mp3 -acodec copy output.mp3
which merges file1.mp3 and file2.mp3 to become output.mp3.
The problem is that I have a lot more than 2 files that I would like to merge.
Example
ffmpeg -i concat:file1.mp3\|file2.mp3 -acodec copy output1.mp3
ffmpeg -i concat:output1.mp3\|file3.mp3 -acodec copy output2.mp3
ffmpeg -i concat:output2.mp3\|file4.mp3 -acodec copy output3.mp3
ffmpeg -i concat:output3.mp3\|file5.mp3 -acodec copy output4.mp3
output4.mp3 is the result I am looking for.
The files are not actually nicely called "file" adn then a number, but ls lists them in the order they should be merged in.
Question
How can this be scripted, so I can execute it in a directory with either an even or odd number of files?
if ffmpeg supports more then two files and no file contains |, and there are not too many, you can do:
ffmpeg -i concat:"$(ls|tr '\n' '|')" -acodec copy out.mp3
if not:
for cfile in *.mp3; do
ffmpeg -i concat:myout.mp3tmp1\|$cfile -acodec copy myout.mp3tmp2
mv myout.mp3tmp2 myout.mp3tmp1
done
mv myout.mp3tmp1 <your final file name>
If you can just concatenate all files in one wash, that'd be best. But a generic answer for your Bash question:
ffmpeg -i concat:file1.mp3\|file2.mp3 -acodec copy output1.mp3
for i in $(seq 1 10); do
ffmpeg -i concat:output${i}.mp3\|file$((i + 2)).mp3 -acodec copy output$((i + 1)).mp3
done
Here 10 is two less than your total number of input files.

Resources