Browse code

ffmpeg: make -aspect work with -vcodec copy.

Nicolas George authored on 2013/04/09 18:21:47
Showing 3 changed files
... ...
@@ -468,6 +468,10 @@ form @var{num}:@var{den}, where @var{num} and @var{den} are the
468 468
 numerator and denominator of the aspect ratio. For example "4:3",
469 469
 "16:9", "1.3333", and "1.7777" are valid argument values.
470 470
 
471
+If used together with @option{-vcodec copy}, it will affect the aspect ratio
472
+stored at container level, but not the aspect ratio stored in encoded
473
+frames, if it exists.
474
+
471 475
 @item -vn (@emph{output})
472 476
 Disable video recording.
473 477
 
... ...
@@ -2224,7 +2224,14 @@ static int transcode_init(void)
2224 2224
                 codec->width              = icodec->width;
2225 2225
                 codec->height             = icodec->height;
2226 2226
                 codec->has_b_frames       = icodec->has_b_frames;
2227
-                if (!codec->sample_aspect_ratio.num) {
2227
+                if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
2228
+                    codec->sample_aspect_ratio   =
2229
+                    ost->st->sample_aspect_ratio =
2230
+                        av_mul_q(ost->frame_aspect_ratio,
2231
+                                 (AVRational){ codec->height, codec->width });
2232
+                    av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
2233
+                           "with stream copy may produce invalid files\n");
2234
+                } else if (!codec->sample_aspect_ratio.num) {
2228 2235
                     codec->sample_aspect_ratio   =
2229 2236
                     ost->st->sample_aspect_ratio =
2230 2237
                         ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
... ...
@@ -1168,7 +1168,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
1168 1168
     AVStream *st;
1169 1169
     OutputStream *ost;
1170 1170
     AVCodecContext *video_enc;
1171
-    char *frame_rate = NULL;
1171
+    char *frame_rate = NULL, *frame_aspect_ratio = NULL;
1172 1172
 
1173 1173
     ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO, source_index);
1174 1174
     st  = ost->st;
... ...
@@ -1180,10 +1180,21 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
1180 1180
         exit(1);
1181 1181
     }
1182 1182
 
1183
+    MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
1184
+    if (frame_aspect_ratio) {
1185
+        AVRational q;
1186
+        if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
1187
+            q.num <= 0 || q.den <= 0) {
1188
+            av_log(NULL, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
1189
+            exit(1);
1190
+        }
1191
+        ost->frame_aspect_ratio = q;
1192
+    }
1193
+
1183 1194
     if (!ost->stream_copy) {
1184 1195
         const char *p = NULL;
1185 1196
         char *frame_size = NULL;
1186
-        char *frame_aspect_ratio = NULL, *frame_pix_fmt = NULL;
1197
+        char *frame_pix_fmt = NULL;
1187 1198
         char *intra_matrix = NULL, *inter_matrix = NULL;
1188 1199
         int do_pass = 0;
1189 1200
         int i;
... ...
@@ -1194,17 +1205,6 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in
1194 1194
             exit(1);
1195 1195
         }
1196 1196
 
1197
-        MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
1198
-        if (frame_aspect_ratio) {
1199
-            AVRational q;
1200
-            if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
1201
-                q.num <= 0 || q.den <= 0) {
1202
-                av_log(NULL, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
1203
-                exit(1);
1204
-            }
1205
-            ost->frame_aspect_ratio = q;
1206
-        }
1207
-
1208 1197
         video_enc->bits_per_raw_sample = frame_bits_per_raw_sample;
1209 1198
         MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
1210 1199
         if (frame_pix_fmt && *frame_pix_fmt == '+') {