5

Today I wanted to obtain a ts file cut preserving everything, so I tried with this command:

ffmpeg -i "original.ts" -map 0 -c copy -t 00:30:0.0 -threads 1 "cutted.ts"

The resulting file have different minor specs I would like to make matching to the original one.

original.ts

[mpeg2video @ 00000000030df040] Invalid frame dimensions 0x0.    
    Last message repeated 2 times    
[mp3 @ 00000000030e0420] Header missing    
[mp3 @ 00000000030df940] Header missing    
    Last message repeated 1 times    
[mpegts @ 00000000030dd500] PES packet size mismatch    
    Last message repeated 2 times    
Input #0, mpegts, from 'original.ts':    
  Duration: 01:14:46.92, start: 64563.955900, bitrate: 5367 kb/s    
  Program 1    
    Stream #0:0[0x201]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 16:15 DAR 4:3], max.    
 10000 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc    
    Stream #0:1[0x28b](eng): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, stereo, s16p, 192 kb/s    
    Stream #0:2[0x2b7](001): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, mono, s16p, 64 kb/s    
    Stream #0:3[0x2b8](eng): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, stereo, s16p, 192 kb/s    
    Stream #0:4[0x241](001): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250

cutted.ts

[mp3 @ 000000000302fa20] Header missing
Last message repeated 1 times
[mp3 @ 00000000030304c0] Header missing
Input #0, mpegts, from 'cutted.ts':
Duration: 00:00:10.08, start: 1.400000, bitrate: 4297 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 16:15 DAR 4:3], max.
10000 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x101](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 192 kb/s
Stream #0:2[0x102](001): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, mono, s16p, 64 kb/s
Stream #0:3[0x103](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 192 kb/s
Stream #0:4[0x104](001): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250

Bitrate displayed obviously changes because the subset of media considered have a different length and this is ok.

Start is not kept, being part of a transport stream I would like to keep that exact start time. Here https://ffmpeg.org/pipermail/ffmpeg-user/2013-July/016109.html they talk about reset_timestamps and initial_offset but rather than specifying them i would like them to be copied automatically.

Streams id changes as well as some minor specs inside the parenthesis but i don't know what they are for.

Is also possible obtain an output file without ffmpeg service_name and service_provider metadata ?

PS: Original ts file was created by Terratec Home Cinema.

PPS: My question did not include intentionally the -ss option because that one is supposed to change the start time, however I'm interested also in a solution with -ss that automatically (if possible) offsets the start time with (output start time = input start time + -ss command offset)

3 Answers 3

2

This seems to work:

ffmpeg -ss N -i input.ts -c copy -copyts -muxdelay 0 -max_delay 0 output.ts

You can add a

-map_metadata 0

to preserve whatever metadata ffmpeg can. But this doesn't seem to carry over all the metadata.

3
  • I tried it but it seems not to work for me :( if i use the -copyts option it give output of 0 kB and an alert. If i add all the other options the start time go to zero and the streams IDs remain untouched as in my previous example. Commented Nov 23, 2015 at 1:42
  • Can you upload a small portion - 10 seconds?
    – Gyan
    Commented Nov 23, 2015 at 4:57
  • This answer works, if you specify -ss as 0 (zero) instead of as N. It carries over the start time encoded in the source .ts file to the output .ts file, where it's stored as the output's start time too. Changing -ss to a non zero value does affect the start time in the output. The output file is about 15 percent larger than the source file, even if no processing of the source file has been done! That seems odd. You could adapt this solution to a case of extracting a part of the source .ts file as the output, presumably. I'm running Windows 7.
    – Ed999
    Commented Jul 17, 2023 at 16:06
1

The following is my solution, for an .mp4 video file, using ffmpeg in Windows 7. This command, run in a .bat batch file, will copy a section of the input file (i.e. extract it as the output file), starting 30 seconds into the input file, with the extract having a duration of 60 seconds, and without re-encoding either stream.

This can be modified for a different video type, such as a .ts stream (transport stream), as required. But don't alter the mapping commands in line 2 as that line is the key to maintaining audio synchronisation (the itsoffset option).

The mapping commands preserve the synchronisation of the audio stream with the video stream, by preventing any change in the offset between them.

The metadata in the input file is copied to the output file, also without modification.

The flags commands are needed to write the output file, as mp4 is a container format. The flags commands recreate the mp4 format of the input file (i.e. the container within which the audio and the video streams are housed and synchronised).

The final two lines will display key data from both the input and output files on screen, after the operation, so that you can compare them and be sure that what you intended to achieve has been achieved.

SET file=movie.mp4

SET mapping=-i "%file%" -itsoffset 0 -i "%file%" -map 0:v -map 1:a -copyts

SET flags=-flags global_header -movflags faststart

ffmpeg %mapping% -c:v copy -c:a copy -map_metadata 0 -ss 30 -t 60 %flags% out.mp4

echo. & echo. ffmpeg  -i "%file%" -f nul
echo. & echo. ffmpeg  -i out.mp4  -f nul

I'm not an expert on the .ts format, but it's not a reliable video format. It doesn't provide the global file data that the .mp4 and .mkv video formats provide, so a .ts file is not easily seekable, is slow to start playing, and may not accurately report some file data (such as the overall duration of the video). Hence it is better tactics to convert from .ts to .mp4 or .mkv because those formats don't have the shortcomings of the .ts format.

To convert from .ts to .mp4 video, just delete line 1 in my above batch script and substitute this: SET file=movie.ts

.

1

ffmpeg overwrites service_name and service_provider with default values:

Service01 and FFmpeg.

You could overwrite it with the original metadata but first you have to retrieve it:

i="input.ts"

# get service_name with ffprobe and store it in a variable
svc_name=$(ffprobe -hide_banner -v error -show_entries program_tags=service_name -of default=nw=1:nk=1 "${i}");\

# get service_provider with ffprobe and store it in a variable
svc_provider=$(ffprobe -hide_banner -v error -show_entries program_tags=service_provider -of default=nw=1:nk=1 "${i}");\

# add service_name and service_provider to new TS
ffmpeg -i "${i}" -c copy -copyts -muxdelay 0 -max_delay 0 -map_metadata 0 -metadata service_name="${svc_name}" -metadata service_provider="${svc_provider}" output.ts

Unfortunately ffprobe's program_tags= doesn't work with service_type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.