Answers.Body: <p>This seems to work:</p>
  
  <pre><code>ffmpeg -ss N -i input.ts -c copy -copyts -muxdelay 0 -max_delay 0 output.ts
  </code></pre>
  
  <p>You can add a</p>
  
  <pre><code>-map_metadata 0
  </code></pre>
  
  <p>to preserve whatever metadata ffmpeg can. But this doesn't seem to carry over all the metadata.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-23T01:42:22.137Z
Answers.Text: 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.
Answers.UserDisplayName: user3450548
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-23T04:57:58.173Z
Answers.Text: Can you upload a small portion - 10 seconds?
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-17T16:06:20.650Z
Answers.Text: 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.
Answers.UserDisplayName: Ed999
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-22T21:18:24.357Z
Answers.LastActivityDate: 2015-11-22T21:18:24.357Z
Answers.UserDisplayName: Gyan
Answers.Body: <p>The following is my solution, for an <strong>.mp4 video file</strong>, using ffmpeg in Windows 7. This command, run in a .bat <em>batch file</em>, 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 <em>without</em> re-encoding either stream.</p>
  
  <p>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 <strong>itsoffset</strong> option).</p>
  
  <p>The mapping commands preserve the synchronisation of the audio stream with the video stream, by preventing any change in the offset between them.</p>
  
  <p>The metadata in the input file is copied to the output file, also without modification.</p>
  
  <p>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).</p>
  
  <p>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.</p>
  
  <pre><code>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. &amp; echo. ffmpeg  -i "%file%" -f nul
  echo. &amp; echo. ffmpeg  -i out.mp4  -f nul
  </code></pre>
  
  <p>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.</p>
  
  <p>To convert from .ts to .mp4 video, just delete line 1 in my above batch script and substitute this:  <strong>SET file=movie.ts</strong></p>
  
  <p>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-28T15:37:37.310Z
Answers.LastActivityDate: 2018-05-28T15:57:45.767Z
Answers.LastEditDate: 2018-05-28T15:57:45.767Z
Answers.UserDisplayName: Ed999
Answers.Body: <p>ffmpeg overwrites <code>service_name</code> and <code>service_provider</code> with default values:</p>
  <p><code>Service01</code> and <code>FFmpeg</code>.</p>
  <p>You could overwrite it with the original metadata but first you have to retrieve it:</p>
  <pre><code>i=&quot;input.ts&quot;
  
  # 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 &quot;${i}&quot;);\
  
  # 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 &quot;${i}&quot;);\
  
  # add service_name and service_provider to new TS
  ffmpeg -i &quot;${i}&quot; -c copy -copyts -muxdelay 0 -max_delay 0 -map_metadata 0 -metadata service_name=&quot;${svc_name}&quot; -metadata service_provider=&quot;${svc_provider}&quot; output.ts
  </code></pre>
  <p>Unfortunately ffprobe's <code>program_tags=</code> doesn't work with <code>service_type</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-02T13:29:18.640Z
Answers.LastActivityDate: 2021-09-02T13:29:18.640Z
Answers.UserDisplayName: drake7
Body: <p>Today I wanted to obtain a ts file cut preserving everything, so I tried with this command:</p>
  
  <pre><code>ffmpeg -i "original.ts" -map 0 -c copy -t 00:30:0.0 -threads 1 "cutted.ts"
  </code></pre>
  
  <p>The resulting file have different minor specs I would like to make matching to the original one.</p>
  
  <h2>original.ts</h2>
  
  <pre><code>[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
  </code></pre>
  
  <h2>cutted.ts</h2>
  
  <blockquote>
    <p>[mp3 @ 000000000302fa20] Header missing<br>
       Last message repeated 1 times<br>
    [mp3 @ 00000000030304c0] Header missing<br>
    Input #0, mpegts, from 'cutted.ts':<br>
     Duration: 00:00:10.08, start: <strong>1.400000</strong>, bitrate: 4297 kb/s<br>
     Program 1<br>
       Metadata:<br>
         service_name    : Service01<br>
         service_provider: FFmpeg<br>
       Stream #0:0[<strong>0x100</strong>]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 16:15 DAR 4:3], max.<br>
    10000 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc<br>
       Stream #0:1[<strong>0x101</strong>](eng): Audio: mp2 (<strong>[3][0][0][0] / 0x0003</strong>), 48000 Hz, stereo, s16p, 192 kb/s<br>
       Stream #0:2[<strong>0x102</strong>](001): Audio: mp2 (<strong>[3][0][0][0] / 0x0003</strong>), 48000 Hz, mono, s16p, 64 kb/s<br>
       Stream #0:3[<strong>0x103</strong>](eng): Audio: mp2 (<strong>[3][0][0][0] / 0x0003</strong>), 48000 Hz, stereo, s16p, 192 kb/s<br>
       Stream #0:4[<strong>0x104</strong>](001): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250</p>
  </blockquote>
  
  <p>Bitrate displayed obviously changes because the subset of media considered have a different length and this is ok.</p>
  
  <p>Start is not kept, being part of a transport stream I would like to keep that exact start time. Here <a href="https://ffmpeg.org/pipermail/ffmpeg-user/2013-July/016109.html" rel="noreferrer">https://ffmpeg.org/pipermail/ffmpeg-user/2013-July/016109.html</a> they talk about <strong>reset_timestamps</strong> and <strong>initial_offset</strong> but rather than specifying them i would like them to be copied automatically.</p>
  
  <p>Streams id changes as well as some minor specs inside the parenthesis but i don't know what they are for.</p>
  
  <p>Is also possible obtain an output file without ffmpeg service_name and service_provider metadata ?</p>
  
  <p>PS: Original ts file was created by Terratec Home Cinema.</p>
  
  <p>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)</p>
  
Tags: <video><ffmpeg>
Title: ffmpeg Cut a media preserving all streams but also all metadata, timecodes and everything else
UserDisplayName: user3450548
ThreadText: # ffmpeg Cut a media preserving all streams but also all metadata, timecodes and everything else
  
  (Asked by: username_0 on 2015-11-22)
  
  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)
  
  **Tags:** video, ffmpeg
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2015-11-22. Score: 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.
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_1**: Can you upload a small portion - 10 seconds?  
  > **username_2**: 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.  
  
  ---
  
  ### Answer by username_2 on 2018-05-28. Score: 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**
  
  .
  
  
  ---
  
  ### Answer by username_3 on 2021-09-02. Score: 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`.
  
  
  ---
  
  