FFMPEG: seeking using a txt file - ffmpeg

I have 40 videos that I need to trim, so they would start at the 6th second and then combine them into a single video.
I also have a list_of_movies.txt that contains the name of all the files I need to trim.
This is my problem:
When concatenating the videos to a single file, ffmpeg accepts:
ffmpeg -f concat -i list_of_movies.txt -c copy output.mp4
but when trimming, it would not accept:
ffmpeg -ss 00:00:06 -i list_of_movies.txt trimmed .mp4
What am I doing wrong?

If you want to trim each video to start from its 6th second, you'll have to specify it for each file within the text file, so
file first.mp4
inpoint 5
file second.mp4
inpoint 5
file third.mp4
inpoint 5
[...]
and then run
ffmpeg -f concat -i list_of_movies.txt -c copy output.mp4
However, in copy mode, ffmpeg can only extract, starting at a video keyframe, so expect extra frames and maybe broken audio sync.
It's best to re-encode,
ffmpeg -f concat -segment_time_metadata 1 -i list_of_movies.txt -vf select=concatdec_select,setpts=N/FRAME_RATE/TB -af aselect=concatdec_select,asetpts=N/SR/TB output.mp4

Related

merge audio and video with ffmpeg doesnt work correctly

I have ubuntu 20.04 and in past days I did this job(merge video and audio) well in terminal and with ffmpeg:
ffmpeg -i input.mp4 -i input2.mp3 -c copy output.mp4
so fast I have recived output.mp4, but now I tried this one and get output without any sound!
I try another ways to merge this ones(also with ffmpeg) but there are no diffrent...
ffmpeg -f concat -safe 0 -i <(for f in ./input*.mp4; do echo "file '$PWD/$f'"; done) -c copy output.mp4
Note -f concat will select a demuxer. This alters the way -i nput files are read.
So instead video-files 'concat expects a txt-file listing the files to concatenate.
However we somehow omit the creation of that text file and use process substitution to generate and pass that list on the fly to demux.
For more details go here:
https://trac.ffmpeg.org/wiki/Concatenate#demuxer
If you want to merge several video files, you can use these command.
merge two video files.
ffmpeg -f concat -i 1.mp4 -1 2.mp4 -codec copy out.mp4
merge multiple video files.
ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mpt -vcodec copy -acodec copy out.mp4

FFmpeg remove 2 sec from middle of video and concat the parts. Single line solution

I have a video file that is 22 seconds long.
I want to remove the segment from 10 seconds to 12 seconds.
Then return a concatenated video file of seconds 1-10 and 12-22.
I want to do this in a single FFmpeg command.
This is the easy way
Source
https://www.labnol.org/internet/useful-ffmpeg-commands/28490/
ffmpeg -i input.mp4 -ss 00:00:00.0 -codec copy -t 10 output_1.mp4
and
ffmpeg -i input.mp4 -ss 00:00:12.0 -codec copy -t 10 output_2.mp4
then create an input file with all the source file names and run
ffmpeg -f concat -i file-list.txt -c copy output.mp4
But I'm looking for a one line solution
Any help would be appreciated.
For exact trimming, you'll have to re-encode
Use
ffmpeg -i input.mp4 -vf select='not(between(t,10,12))',setpts=N/FRAME_RATE/TB -af aselect='not(between(t,10,12))',asetpts=N/SR/TB out.mp4

extract audio using ffmpeg and save wav files

ffmpeg has a way to extract video images using the following command
ffmpeg -i "video.avi" -vf fps=30 "images/frame_%09d.png" -hide_banner
Is there a way in ffmpeg to extract or cut audios into smaller chunks the same way that is done on the above command but the difference is it will create an audio file not an image file.
I tried the following but it only creates a wav file.
ffmpeg -i "video.avi" -vf fps=30 "images/frame_%09d.wav" -hide_banner
Remove -vf option, you don't need it as you don't need video
Video frames can be assumed as single units, but what you want in audio? Sungle samples. huh? So, you must specify interval
For example:
# ffmpeg -i "video.avi" -f segment -segment_time 3 -c copy "images/frame_%03d.mp3" -hide_banner

Concatenate TS files with correct timestamps

I'm trying to merge multiple ts chunk files to one single file, without any loss of quality or reencoding. The files are taken from a live stream, however I'm trying to merge them in a diffrent order and not the order they were streamed.
Example of files:
0000000033.ts
0000000034.ts
0000000039.ts
0000000044.ts
I tried:
cat 0000000033.ts 0000000034.ts 0000000039.ts 0000000044.ts >combined.ts
and
ffmpeg -i "concat:0000000033.ts|concat:0000000034.ts|concat:0000000039.ts|concat:0000000044.ts" -c copy -bsf:a aac_adtstoasc output.mp4
This kinda works, however I instead of beeing 4 seconds long it's around 15. It plays this way:
[first 2 clips]
[5 secs pause]
[39.ts]
[5 secs pause]
[44.ts]
[done]
This happens to both the cat and ffmpeg combined version. So it seems the ts chunks contain timestamps from the stream that are beeing used.
How can I fix that to make it one continous clip?
The chunks here are more of an example, the chunks will be dynamically selected.
If you have a long list of TS files, you can create a playlist, a file containing a list of the TS files in this line format:
file 'seg-37-a.ts'
These commands produce such a file, with the TS files sorted numerically.
delimiterBeforeFileNumber="-"
ls |egrep '[.]ts$' \
|sort "-t$delimiterBeforeFileNumber" -k2,2n \
|sed -r "s/(.*)/file '\1'/" >ts.files.txt
Then the creation of the single file can read the playlist using the -f concat modifier of ffmpeg's -i option.
ffmpeg -f concat -i ts.files.txt -c copy tsw.014.ts.mp4
Haven't checked whether this works with the concat protocol, but you need to generate a new set of timestamps.
ffmpeg -i "concat:0000000033.ts|0000000034.ts|0000000039.ts|0000000044.ts" \
-c copy -bsf:a aac_adtstoasc -fflags +genpts output.mp4
TS files can actually be merged with the Windows 'copy' command. The following will merge every TS in the current folder. Then, once you have a single ts, transmux to mp4 without re-encoding. I confirm the video duration will be correct unlike ffmpeg's concat.
copy /b *.ts all.ts
ffmpeg -i all.ts -c copy all.mp4
In recent versions of ffmpeg, a bit stream filter setts has appeared that allows you to fix timestamps without transcoding. Try it, in my case it helped.
-bsf "setts=PTS-STARTPTS;DTS-STARTDTS"
I am combining a lot of mpeg ts chunks using unix cat, and then transmuxing to an mp4 container
ffmpeg -abort_on empty_output_stream -hide_banner -loglevel repeat+level+error -progress pipe:1 -i pipe:0 -map 0 -c copy -bsf:a aac_adtstoasc -bsf "setts=PTS-STARTPTS;DTS-STARTDTS" -f mp4 -movflags faststart -y out.mp4
Are the examples accurate in file numbers? as 0033.ts and 0034.ts play together but it takes 5 secs to get to 0039.ts and then another 5 to 0044.ts so 0034 + 5 secs = 0039 and + 5 secs = 0044 so are you joining them in their proper order?
Sorry I misread the question but in regards to your problem once you have the 15 sec clip there is a program called flv editor lite from moyea which will take in and convert a .mp4 file to .flv and allow you to cut the excess time out of the file and export it as one file but then you need to reconvert back to .mp4 again

How to Loop Input video x number of time using FFMPEG?

I want to loop same video 4 times and output as video using ffmpeg.
SO I create code like this in ffmpeg.
ffmpeg -loop 4 -i input.mp4 -c copy output.mp4
but when i run it it give the error like this.
Option Loop Not Found.
how to do this withour error. Please Help Me
In recent versions, it's
ffmpeg -stream_loop 4 -i input.mp4 -c copy output.mp4
Due to a bug, the above does not work with MP4s. But if you wrap to a MKV, it works for me.
ffmpeg -i input.mp4 -c copy output.mkv
then,
ffmpeg -stream_loop 4 -i output.mkv -c copy output.mp4
I've found an equivalent work-around with input concatenation for outdated/bugged versions -stream_loop:
ffmpeg -f concat -safe 0 -i "video-source.txt" -f concat -safe 0 -i "audio-source.txt" -c copy -map 0:0 -map 1:0 -fflags +genpts -t 10:00:00.0 /path/to/output.ext
This will loop video and audio independently of each other and force-stop the output at 10 hour mark.
Both text files consist of
file '/path/to/file.ext'
but you must make sure to repeat this line enough times to keep the output satisfied.
For example, if your total video time is less than total audio time then video output will stop earlier than intended and the audio will keep playing until either -t 10H is reached or audio ends prematurely.

Resources