How to split and join without transcoding AVC/MPEG-TS video files in Windows with ffmpeg?
How do I split and join files using ffmpeg
while retaining all audio tracks?
As you have discovered, a bitstream copy will select only one (audio) track, as per the stream specification documentation:
By default,
ffmpeg
includes only one stream of each type (video, audio, subtitle) present in the input files and adds them to each output file. It picks the "best" of each based upon the following criteria: for video, it is the stream with the highest resolution, for audio, it is the stream with the most channels, for subtitles, it is the first subtitle stream. In the case where several streams of the same type rate equally, the stream with the lowest index is chosen.
To select all audio tracks:
ffmpeg -i InputFile.ts-c copy -ss 00:12:34.567 -t 00:34:56.789 -map 0:v -map 0:a FirstFile.ts
To select the third audio track:
ffmpeg -i InputFile.ts -c copy -ss 00:12:34.567 -t 00:34:56.789 -map 0:v -map 0:a:2 FirstFile.ts
You can read more about and see other examples of stream selection in the advanced options section of the ffmpeg
documentation.
I would also combine -vcodec copy -acodec copy
from your original command into -c copy
as above for compactness of expression.
Split:
So, combining those with what you want to achieve in the two files in terms of splitting for later re-joining:
ffmpeg -i InputOne.ts -ss 00:02:00.0 -c copy -map 0:v -map 0:a OutputOne.ts
ffmpeg -i InputTwo.ts -c copy -t 00:03:05.0 -map 0:v -map 0:a OutputTwo.ts
will give you:
-
OutputOne.ts
, which is everything after the first two minutes of the first input file -
OutputTwo.ts
, which is the first 3 minutes and 5 seconds of the second input file
Join:
ffmpeg
supports concatenation of files without re-encoding, described extensively in its concatenation documentation.
Create your listing of files to be joined (eg join.txt
):
file '/path/to/files/OutputOne.ts'
file '/path/to/files/OutputTwo.ts'
Then your ffmpeg
command can use the concat demuxer
:
ffmpeg -f concat -i join.txt -c copy FinalOutput.ts
Since you are working with mpeg
transport streams (.ts
), you should be able to use the concat protocol as well:
ffmpeg -i "concat:OutputOne.ts|OutputTwo.ts" -c copy -bsf:a aac_adtstoasc output.mp4
Per the example on the concat page linked above. I'll leave that up to you to experiment with.
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin almost 2 years
I am looking also for a Linux solution (in a separate question)
I have two recorded video files with the extension
.ts
. I need to remove some part of the first, split the second, and then merge the first file with the first (split) part of the second file.They both have the same characteristics:
Format : MPEG-TS File size : 3.16 GiB Duration : 1h 39mn Overall bit rate mode : Variable Overall bit rate : 4 527 Kbps Video ID : 720 (0x2D0) Menu ID : 6181 (0x1825) Format : AVC Format/Info : Advanced Video Codec Format profile : High@L4 Format settings, CABAC : Yes Format settings, ReFrames : 4 frames Codec ID : 27 Duration : 1h 39mn Width : 1 440 pixels Height : 1 080 pixels Display aspect ratio : 16:9 Frame rate : 25.000 fps Color space : YUV Chroma subsampling : 4:2:0 Bit depth : 8 bits Scan type : MBAFF
They also contain multiple audio tracks and subtitles.
I want to process them without transcoding the files, at least be able to remove the beginning etc.
FFMPEG
:ffmpeg -i source.mts
gives:Input #0, mpegts, from 'source1.mts': Duration: 01:40:00.76, start: 2995.511878, bitrate: 4526 kb/s Program 6181 Stream #0:0[0x46](fra,fra): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006) Stream #0:1[0x2d0]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 1440x1080 [SAR 4:3 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc Stream #0:2[0x2da](fra): Audio: aac (HE-AAC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 73 kb/s Stream #0:3[0x2db](qaa): Audio: aac (HE-AAC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 67 kb/s Stream #0:4[0x2dc](deu): Audio: aac (HE-AAC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 49 kb/s Stream #0:5[0x2dd](qad): Audio: aac (HE-AAC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp, 57 kb/s
I think it is very well doable with
ffmpeg
, which is also cross-platform, as a command that I found here to remove a part of one file.ffmpeg -i InputFile -vcodec copy -acodec copy -ss 00:12:34.567 -t 00:34:56.789 OutputFile
works, excepting the fact that the input has four audio tracks and in this way only the first (
Stream #0:2[0x2da](fra)
) audio is taken. What I want is: either all audio, either the third (Stream #0:3[0x2db](qaa)
). How to adjust?ffmpeg
should therefore work to:1. Cut off the beginning of the first file (remove first 2 minutes out of 1h40 min file)
2. Split a second (1 hour) file in two parts (after 3.05 minutes)
3. Join (append) to the end of the first file the 3.05 min part obtained from splitting the second file.
What I have tried:
Openshot: cannot process without transcoding.
Avidemux (Linux): when trying to save it says "This is not MPEG compatible - You cannot use the Copy codec."
Avidemux in Windows: audio is out of sync.
-
bertieb over 8 yearsI forgot to include the video
-map
ping, thanks for the reminder. I have updated the question on how to join (concatenate) video files together as per your edit. -
bertieb over 8 yearsMy edit includes a section on joining files. I'm still not sure what you mean by the 'split' but I'm glad you have a command that works :)
-
bertieb over 8 yearsHow many outputs do you want? One, the final joined file? Or do you need both parts of the split on the second file kept?
-
bertieb over 8 yearsTo keep all streams (video, audio, subtitles), replace all
-map
s with the single:-map 0
-
Admin over 8 yearsplease edit the
join.txt
content: the path to files has to be in reversed comas to work. but none of the solutions for joining allows keeping all audio, as only default/first is selected -
bertieb over 8 yearsGood catch, I could swear I had those in in a previous revision. Relative paths will also work.
-
Admin over 8 yearsis there a way to join the files with all audio? they are lost except the default one when joining. or i'll select at a previous stage the one preferred and use that in both files to be joined
-
bertieb over 8 yearsIf the
concat
demuxer also only grabs the 'best' track ( would expect it to work differently, but then this isffmpeg
...), I would include the-map 0
in that step as well. -
Admin over 8 yearsinteresting, a comment under here saying
-ss
should be used before-i
, while-t
should be after-i
. Also: trac.ffmpeg.org/wiki/Seeking and askubuntu.com/a/547995/47206 -
bertieb over 8 yearsThe positioning of
ffmpeg
commands is very important.-ss
can be used either before or after-i
for slightly different methods of achieving the same thing (frame-accurate seeking). You can alternatively use-to
if you prefer to specify an end time, rather than a duration (-t
). -
Admin over 8 yearsreplacing
ffmpeg
withavconv
works ok I guess in Linux. Is it available for Windows too? -
bertieb over 8 years
ffmpeg
is available for Windows systems via Zeranoe, yes. I'd rather not get intoffmpeg
vsavconv
, but see superuser.com/questions/507386/… and stackoverflow.com/questions/9477115/… -
Admin over 8 yearsAfter "To select all audio tracks:", change the command by adding a space after
InputFile.ts